13

I need to process an exception message when any exception is caught in Java.

I am working on a Database Connection class. When I give wrong details like username, password, host name, sid, etc, the control goes to catch block and gives an error. I would like to get this error message on the JSP side and redirect to the same page with that error message. However, when I get the error message in Java, it always takes a null value.

My code example 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 to Exception block but the errorMessage is null.

3 Answers 3

17

At first, the answer of @Artem Moskalev should be right in most ways. In your case, you'd said:

It will goes to Exception block but the errorMessage is null.

So, let's try two cases to debug the behavior:

First:

class Test1
{
    public static void main (String[] args) throws java.lang.Exception
    {
        String errorMessage = null;
            try{
                throw(new Exception("Let's 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's throw some exception message here

Seems to work like you expected.


Second:

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 that? Because you're accessing e.getMessage(), but if the message is emtpy e.getMessage() will return null. So the null isn't from the initialization, but from the return value of e.getMessage(), when e doesn't have any error message (for example if there is a NullPointerException thrown).

Sign up to request clarification or add additional context in comments.

Comments

3

your catch(Exception e) block will catch all exceptions (other than SQLException which you are specifically catching above). Some exceptions e.g. NullPointerException can have null details message i.e. e.getMessage() can retun null. So it is better to print the exception details like exception type also- use e.ToString() or e.printStacktrace() for more details.

Comments

2

This block is always executed:

...finally{
    System.out.println(errorMessage);
}

If your errorMessage has not been assigned any other value before (ie. no exception thrown in your try clause) - System.out.println will print you the errorMessage value which is null.

3 Comments

The control goes to Exception block as the print line in Exception block is printed on output, And when i use printStacktrace it will give me NullPointerException. But still the variable errorMessage is null.
Do you mean when you call printStackTrace on your exception? It should write you the reason for the exception unless its your own custom exception and you do not supply any message to it.
@Maheshwari check my answer below. You are getting a NullPointerException which has a null details message docs.oracle.com/javase/7/docs/api/java/lang/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.