-1

Possible Duplicate:
Is it a bad practice to catch the Throwable?

Is it best practice to catch throwable? If catching throwable, will it catch out of memory etc exception?

Community
  • 1
  • 1
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 2
    http://stackoverflow.com/search?q=%5Bjava%5D+catch+throwable – Andrew Marshall Jun 17 '12 at 02:57
  • Although this might be more appropriate: http://stackoverflow.com/questions/581878/why-catch-exceptions-in-java-when-you-can-catch-throwables –  Jun 17 '12 at 02:58

1 Answers1

0

The only way to catch Throwable exception, is the same as that you catch Exceptions. How it works is, it catch the exceptions/error by the hierarchy tree.

So if you wanna catch OutOfMemory Error you have some options:

try{
}catch(java.lang.OutOfMemoryError t){
}

try{
}catch(java.lang.Error t){
}

try{
}catch(java.lang.Throwable t){
}

Just check the class tree in the docs for the exception that you wanna catch. http://docs.oracle.com/javase/6/docs/api/java/lang/OutOfMemoryError.html

Also a good practice is among your catch blocks always start with the ones with are in the bottom of the tree, so for example:

try{
//
}catch(java.lang.OutOfMemoryError t){
// handle out of memory error
}catch(java.lang.Throwable t){
// handle other throwable
}

Also remember that Error and Exception they both extend Throwable, but they dont extend each other so both are siblings in the class tree.

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Arthur Neves
  • 11,840
  • 8
  • 60
  • 73