How do I get an exception message in a string variable in Java?
I need to handle an exception message when any exception is caught in Java.
I am working on a database connection class. When I give incorrect data like username, password, hostname, sid, etc., Control goes to the catch block and throws an error. I would like to receive this error message on the JSP side and redirect to the same page with this error message. However, when I get an error in Java, it always evaluates to null.
My sample code is here.
String errorMessage = null;
try{
// CODE Where Exception occure
}catch(SQLException se){
errorMessage = se.getMessage();
}catch(Exception e){
System. out.println("In Exception block.");
errorMessage = e.getMessage();
}finally{
System.out.println(errorMessage);
}
It will go into the Exception block, but the errorMessage will be null.
First, @ Artem Moskalev's answer should be correct in many ways. In your case, you would say:
It goes into the exception block, but errorMessage is null.
So let's try two cases to debug the behavior:
Firstly:
class Test1
{
public static void main (String[] args) throws java.lang.Exception
{
String errorMessage = null;
try{
throw(new Exception("Let throw some exception message here"));
}catch(Exception e){
System.out.println("In Exception block.");
errorMessage = e.getMessage();
}finally{
System.out.println(errorMessage);
}
}
}
Output:
In Exception block. Let throw some exception message here
Seems to work as you'd expect.
Secondly:
class Test2
{
public static void main (String[] args) throws java.lang.Exception
{
// String errorMessage = null;
// To make the difference between non-initialized value
// and assigned null value clearer in this case,
// we will set the errorMessage to some standard string on initialization
String errorMessage = "Some standard error message";
try{
throw(new Exception());
}catch(Exception e){
System.out.println("In Exception block.");
errorMessage = e.getMessage();
}finally{
System.out.println(errorMessage);
}
}
}
Output:
In Exception block. null
Why is this? Because you access e.getMessage()
, but if emtpy message, e.getMessage()
will return null
. That way, null
not from initialization, but from the return value e.getMessage()
when e
it has no error message (for example if thrown NullPointerException
).
This block is always executed:
...finally{
System.out.println(errorMessage);
}
If yours errorMessage
was not previously assigned any other value (i.e. the exception thrown in your proposal try
) - System.out.println
will print you the value errorMessage
that is null
.
your block catch(Exception e)
will catch all exceptions (except SQLException
which you specifically catch above). Some exceptions, for example. NullPointerException
can have a message with a null message, i.e. e.getMessage()
can reset zero. Therefore, it is better to print the details of the exception, such as the type of the exception - use e.ToString()
or e.printStacktrace()
for more details.
Use is getMessage()
useless, you want either e.printStackTrace()
(for really simple programs) or for any correct error handling use a registration framework that allows you to write code like log.error("Something went wrong", e);
.
try {
throw(new Exception());
} catch (Exception e) {
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
stringWriter.toString();
}