JAVASE study notes -009 exception handling

A, Java exception hierarchy

  1. Throwable
    1. Error
    2. Exception
      1. RuntimeException
      2. 非RuntimeException

Java exceptions and compiled with abnormal into operation.
An exception only to be found in the program is running is running.
Compiled abnormalities can be found at compile time, and have to deal with.
Custom exceptions are usually compiled an exception.

Second, the statement and throw an exception

1
2
3
4

Method name () throws Exception Type 1 , Type exception 2 { the throw exception object }


Third, the capture and handle exceptions

  1. Call declares compiled unusual methods must be abnormal capture and processing
  2. Use try ... catch catch unhandled exceptions
    1. Finally block of code must be performed
  3. Or continue to call the method declaration throws an exception, handled by the upper caller
. 1 
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
large column   JAVASE study notes -009 exception handling S = "Line"> 10
. 11
12 is
13 is
14
15
16
the try { // code may throw exceptions } catch (IOException E) { // IO exception handling } catch (Exception E) {     // plurality of nested catch, the latter type of exception range greater // other exception handling } a finally { // statement will be executed }









//JDK1.7 above new features
the try {

} The catch (Exception Type 1 | exception type 2 E) { // an abnormal and abnormal 2 can not have inheritance

}

Abnormal commonly used method

  1. getMessage () Gets the exception information
  2. printStack () method call stack print anomalies

Fourth, the custom exception

  1. Custom exception must be directly / indirectly inherit Exception class
  2. Usually in the Java predefined exception can not meet the requirements, it will be custom exception.

Guess you like

Origin www.cnblogs.com/lijianming180/p/12389227.html