19

In Java, we handle exceptions using try catch blocks. I know that I can write a try catch block like the one below to catch any exception thrown in a method.

try {
  // do something
}
catch (Throwable t) {

}

But is there any way in Java which allows me to get a specific method called when an exception happens, instead of writing a catch-all method like the one above?

Specifically, I would like to show a user friendly message in my Swing application when an exception is thrown (which is not handled by my application logic).

Thanks.

2
  • You should never catch Throwable, catch Exception instead. Throwable includes Error and it's descendants which normally cannot be handled Commented Jun 5, 2011 at 15:42
  • 1
    I want to be able to give a friendly error message to the user through Swing. That's why I included Throwable. Even in case of an Error, I want my user to be known about the incident without it just going into my log file. Commented Jun 5, 2011 at 15:44

4 Answers 4

33

By default, the JVM handles uncaught exceptions by printing the stack-trace to System.err stream. Java allows us to customize this behavior by providing our own routine which implements Thread.UncaughtExceptionHandler interface.

Take a look at this blog article which I wrote sometime back which explains this in detail ( http://blog.yohanliyanage.com/2010/09/know-the-jvm-1-uncaught-exception-handler/ ).

In summary, all you have to do is write your custom logic as below :

public class MyUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
  public void uncaughtException(Thread t, Throwable e) {
     // Write the custom logic here
   }
}

And set it using any of the three options I have described in the above link. For example, you could do the following to set the default handler for the entire JVM (so any uncaught exception thrown will be handled by this handler).

Thread.setDefaultUncaughtExceptionHandler(new MyUncaughtExceptionHandler() );
Sign up to request clarification or add additional context in comments.

Comments

0

Show the friendly message from within the catch block.

1 Comment

I am looking at an alternative to writing try-catch blocks all over in my application. Also, I am searching for a better alternative to a single catch-all block in my main method to handle this. Currently it seems to me that I have to write a catch-all block in each of my threads to deal with this.
0

you could wrap each method that can throw in a try catch

or use the getStackTrace()

catch (Throwable t) {
    StackTraceElement[] trace = t.getStackTrace();
    //trace[trace.length-1].getMethodName() should contain the method name inside the try
}

btw catching throwable in not recommended

1 Comment

I am looking for an alternative to writing try-catch blocks everywhere as I have mentioned in the question. Also, I want to be able to give a friendly error message to the user through Swing. That's why I included Throwable. Even in case of an Error, I want my user to be known about the incident without it just going into my log file.
0
try {
   // do something
   methodWithException();
}
catch (Throwable t) {
   showMessage(t);
}

}//end business method

private void showMessage(Throwable t){
  /* logging the stacktrace of exception
   * if it's a web application, you can handle the message in an Object: es in Struts you can use ActionError
   * if it's a desktop app, you can show a popup
   * etc., etc.
   */
}

1 Comment

I am looking at an alternative to writing try-catch blocks all over in my application. Also, I am searching for a better alternative to a single catch-all block in my main method to handle this. Currently it seems to me that I have to write a catch-all block in each of my threads to deal with this

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.