Difference between using Throwable and Exception in a try catch

后端 未结 5 1384
忘了有多久
忘了有多久 2020-11-27 09:03

Sometimes, I see

try {

} catch(Throwable e) {

}

And sometimes

try {

} catch(Exception e) {

}

What is

5条回答
  •  生来不讨喜
    2020-11-27 10:02

    Thowable 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.

提交回复
热议问题