Difference between using Throwable and Exception in a try catch

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

Sometimes, I see

try {

} catch(Throwable e) {

}

And sometimes

try {

} catch(Exception e) {

}

What is

5条回答
  •  感情败类
    2020-11-27 09:52

    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.

提交回复
热议问题