445

Sometimes, I see

try {

} catch(Throwable e) {

}

And sometimes

try {

} catch(Exception e) {

}

What is the difference?

Nubok
  • 3,502
  • 7
  • 27
  • 47
jax
  • 37,735
  • 57
  • 182
  • 278
  • 3
    Related: http://stackoverflow.com/questions/2129647/exception-vs-throwable-in-java http://stackoverflow.com/questions/498217/when-should-throwable-be-used-instead-of-new-exception – Jørn Schou-Rode Feb 16 '10 at 15:56

6 Answers6

352

By catching Throwable it includes things that subclass Error. You should generally not do that, except perhaps at the very highest "catch all" level of a thread where you want to log or otherwise handle absolutely everything that can go wrong. It would be more typical in a framework type application (for example an application server or a testing framework) where it can be running unknown code and should not be affected by anything that goes wrong with that code, as much as possible.

Maurice Perry
  • 9,261
  • 2
  • 12
  • 24
Yishai
  • 90,445
  • 31
  • 189
  • 263
  • 50
    Probably would be best to explain a bit of the hierarchy here. – Xonatron Feb 06 '12 at 20:56
  • 18
    Context for this answer: Throwable includes both Error and Exception as subclasses, so first try/catch is inclusive of second but usually over-broad. – T_T Feb 11 '15 at 16:17
  • 4
    It also includes user-defined direct subclasses of Throwable and instances of Throwable itself. There's nothing stopping you from writing `throw new Throwable();`, so it is the only way to truely catch everything. – Antimony Mar 21 '16 at 00:50
  • 6
    Although accepted, this is not answering the question because most of the answer describes a best practice to catching both Exception and Throwable, and the question was about the difference (as in when to use which when I DO want either). "It includes things that subclass Error" is the only difference specified and it's really a comprehensive answer: What is Error? Why does it matter that it includes it? Any other differences or best practices? – Oded Niv Oct 07 '19 at 07:15
  • @OdedNiv "What is Error? Why does it matter that it includes it?" you can ask those in another question. – Kronen Nov 05 '19 at 09:13
265

The first one catches all subclasses of Throwable (this includes Exception and Error), the second one catches all subclasses of Exception.

Error is programmatically unrecoverable in any way and is usually not to be caught, except for logging purposes (which passes it through again). Exception is programmatically recoverable. Its subclass RuntimeException indicates a programming error and is usually not to be caught as well.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 56
    Quite amazingly, 4 years after this answer, most "code analysis" tools will still report catching throwable as a *critical* error. Logging is a very valid reason for catching Throwable. Years of developing servers tell me that 1) Logging will happen despite getting an `Error` and 2) Unless there is logging, you may never get notified that an OOM happened, leaving you wondering why the server started behaving "funny" – Bruno Grieder Sep 10 '14 at 09:24
  • 4
    What does `programmatically unrecoverable` mean exactly? Is it so severe, that we can't basically call ANY Java method after catching it anymore (logging, etc) without the chance to get an unpredictable behavior from JVM as a result? – Alexander Abakumov Aug 08 '17 at 22:49
  • `Its subclass RuntimeException indicates a programming error`: Not sure if I agree with this statement. If that is true, it means all expected exceptions should be checked exceptions. What if I expect something might fail and is unrecoverable by my application, but I wish to at least throw a meaningful exception? Using a checked exception in that case seems useless and creates boilerplate code. – Nom1fan Apr 18 '19 at 09:52
43

Throwable is super class of Exception as well as Error. In normal cases we should always catch sub-classes of Exception, so that the root cause doesn't get lost.

Only special cases where you see possibility of things going wrong which is not in control of your Java code, you should catch Error or Throwable.

I remember catching Throwable to flag that a native library is not loaded.

Sai Kishore
  • 326
  • 1
  • 7
  • 16
rai.skumar
  • 10,309
  • 6
  • 39
  • 55
41

Throwable catches really everything even ThreadDeath which gets thrown by default to stop a thread from the now deprecated Thread.stop() method. So by catching Throwable you can be sure that you'll never leave the try block without at least going through your catch block, but you should be prepared to also handle OutOfMemoryError and InternalError or StackOverflowError.

Catching Throwable is most useful for outer server loops that delegate all sorts of requests to outside code but may itself never terminate to keep the service alive.

Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
x4u
  • 13,877
  • 6
  • 48
  • 58
36

I feel like this should be here:

Java Exception Hierarchy Image

( Alt link for the img )

Source: https://www.tutorialspoint.com/java/java_exceptions.htm

Tameem Khan
  • 577
  • 5
  • 14
3

I have seen people use Throwable to catch some errors that might happen due to infra failure/ non availability.

Spear A1
  • 525
  • 1
  • 7
  • 20