Java SQLException getStringFromException(java.lang.Throwable exception)

Here you can find the source of getStringFromException(java.lang.Throwable exception)

Description

Gets a String message from an Exception.

License

Open Source License

Parameter

Parameter Description
exception to be read and transformed into a message to print

Return

the message to output

Declaration

public static String getStringFromException(java.lang.Throwable exception) 

Method Source Code

//package com.java2s;
/**********************************************************************
Copyright (c) 2003 Andy Jefferson and others. All rights reserved. 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at/*from  w w w. ja va 2s  .c o  m*/
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
     
    
Contributors:
2003 Erik Bengtson - moved replaceAll from Column class to here
2004 Andy Jefferson - moved intArrayToString, booleanArrayToString from SM
2007 Xuan Baldauf - toJVMIDString hex fix
...
**********************************************************************/

import java.io.PrintWriter;
import java.io.StringWriter;

import java.lang.reflect.InvocationTargetException;

import java.sql.SQLException;

public class Main {
    /**
     * Gets a String message from an Exception. 
     * This method transforms nested exceptions into printable messages.
     * @param exception to be read and transformed into a message to print
     * @return the message to output
     */
    public static String getStringFromException(java.lang.Throwable exception) {
        StringBuilder msg = new StringBuilder();
        if (exception != null) {
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            exception.printStackTrace(printWriter);
            printWriter.close();
            try {
                stringWriter.close();
            } catch (Exception e) {
                //do nothing
            }
            msg.append(exception.getMessage());
            msg.append('\n');
            msg.append(stringWriter.toString());

            // JDBC: SQLException
            if (exception instanceof SQLException) {
                if (((SQLException) exception).getNextException() != null) {
                    msg.append('\n');
                    msg.append(getStringFromException(((SQLException) exception).getNextException()));
                }
            }
            // Reflection: InvocationTargetException
            else if (exception instanceof InvocationTargetException) {
                if (((InvocationTargetException) exception).getTargetException() != null) {
                    msg.append('\n');
                    msg.append(
                            getStringFromException(((InvocationTargetException) exception).getTargetException()));
                }
            }
            // TODO Add more exceptions here, so we can provide complete information in the log
        }
        return msg.toString();
    }
}

Related

  1. getNextExceptionFromLastCause(Exception e)
  2. getSingleSQLExceptionCause(SQLException e)
  3. getSQLErrorCode(SQLException ex_)
  4. getSQLExceptionMessage(SQLException ex)
  5. getSqlStateString(SQLException se)
  6. isConnectionError(final SQLException error)
  7. isConstraintViolation(SQLException e)
  8. isDatabaseAlreadyExistsException(Exception e)
  9. isDataConversionException(SQLException se)