Skip to main content

How Can I Catch All Possible Exceptions in Java?

--
All exceptions come from the "mother class" called java.lang.Throwable and one of two subclasses called java.lang.Error and java.lang.Exception. A block of code that is executed when an exception occurs is called an Exception handler. By catching java.lang.Throwable, it is possible to handle all unexpected conditions.
...
try {
}
catch(Throwable e) {
...
}
...
There are some special exceptions that used by the JVM, those are the sub-classes of java.lang.Error. We are not suppose the catch them in our real code and we usually catch java.lang.Exception for all application and runtime exceptions.
...
try {
}
catch(Exception e) {
...
}
...

Comments

  1. You know this is plain wrong, right? Pokemon exception handling is the worst thing you can do (about exceptions, obviously).

    ReplyDelete
  2. Yes..you are right dear...just i want to tell the all possible ways t catch all exceptions..

    ReplyDelete
  3. Say you catch a Throwable and you want to rethrow it, you can call Thread.currentThread().stop(t). This triggers the current thread to throw the Throwable t. Note: this bypasses the compilers checked exception, so handle with care.

    ReplyDelete

Post a Comment