Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Exceptions - Handling exceptions without try catch

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.

like image 277
Devanath Rao Avatar asked Jun 05 '11 15:06

Devanath Rao


People also ask

Can we throw exception without try catch?

Yes it is Ok to throw an exception when it isn't inside a try block. All you have do is declare that your method throws an exception. Otherwise compiler will give an error. You don't even have to do that if your CapacityExceededException extends Runtime Exception.

Can we handle exception without catch block?

Yes, it is possible. You can use an uncaught exception handler. Its responsibility is to catch the exceptions that your program didn't catch, and do something with it.

How do you handle errors without try catch?

The best alternative I can give you is Elmah. http://code.google.com/p/elmah/ It will handle all your uncaught errors and log them. From there I would suggest fixing said errors or catching the specific errors you expect and not just catch any error that might occur.

How many ways can you handle exceptions in Java?

There are mainly two types of exceptions: checked and unchecked.


2 Answers

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() );
like image 115
Yohan Liyanage Avatar answered Sep 25 '22 20:09

Yohan Liyanage


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.
   */
}
like image 32
alepuzio Avatar answered Sep 24 '22 20:09

alepuzio