Java defines two types of exceptions:
- Checked exception: Inherit from Exception class is Checked exception. The code needs to handle the checked exception that the API throws, either with a catch statement or directly with a throws statement.
- Unchecked exception: Also known as runtimeexception, it is also inherited from exception. But all runtimeexception subclasses have a feature, that is, the code does not need to handle their exceptions can also be compiled, so they are called unchecked exception. RuntimeException (Run-time exception) does not require try...catch ... or throws mechanism to handle the exception.
The inheritance level of the nullpointerexception.
NullPointerException inherits from RuntimeException, so it is a unchecked exception.
The most commonly used five kinds of runtimeexception:
ArithmeticException |
int a=0; int b= 3/a; |
ClassCastException: |
Object x = new Integer (0); System.out.println ((String) x); |
Indexoutofboundsexception ArrayIndexOutOfBoundsException, Stringindexoutofboundsexception |
int [] numbers = {1, 2, 3}; int sum = numbers[3]; |
IllegalArgumentException NumberFormatException |
int a = Interger.parseint ("test"); |
Nullpointerexceptionextends |
|
Summary:
Check exception: Do not handle compilation cannot pass
Non-inspection exception: Do not handle compilation can pass if there is thrown directly to the console.
Run-time exception: is a non-check exception
Non-run-time exception: is an inspection exception
The difference between checked exception and unchecked exception in Java