Why are exceptions named checked and unchecked?

后端 未结 4 2005
情深已故
情深已故 2020-12-10 15:31

Why is the two exception types in Java named \"checked\" and \"unchecked\"? What is the reason behind those names?

4条回答
  •  心在旅途
    2020-12-10 16:04

    In Java you can throw any Throwable. Throwable has two sub-classes: Error and Exception. When an Error is thrown a serious problem has occured that often has very little to do with your code. Such exceptions are not checked by the compiler and is an example of an unchecked exception.

    Exception has a subclass called RuntimeException, those are often exceptions that indicate bugs in your code and can often occur in lots of places in most code. Examples are NullPointerException, ArrayIndexOutOfBoundsException, etc. These are also unchecked since you would litter your code with catches of these.

    All other exceptions are checked since by the compiler and you must catch or throw them.

提交回复
热议问题