AYE89

coding & learning

导航

Difference between java.lang.RuntimeException and java.lang.Exception

Posted on 2019-07-04 06:49  AYE89  阅读(178)  评论(0编辑  收藏  举报

In Java, there are two types of exceptions: checked exceptions and un-checked exceptions. A checked exception must be handled explicitly by the code, whereas, an un-checked exception does not need to be explicitly handled.

For checked exceptions, you either have to put a try/catch block around the code that could potentially throw the exception, or add a "throws" clause to the method, to indicate that the method might throw this type of exception (which must be handled in the calling class or above).

Any exception that derives from "Exception" is a checked exception, whereas a class that derives from RuntimeException is un-checked. RuntimeExceptions do not need to be explicitly handled by the calling code. If RuntimeExceptions aren't caught in the code, they will be handled by JVM.

 

e.g common RuntimeExceptions: 

NullPointerExceptionArrayIndexOutOfBoundException, ArrayIndexOutOfBoundsException, ClassCastException

https://stackoverflow.com/questions/2190161/difference-between-java-lang-runtimeexception-and-java-lang-exception