Abnormal 1 (Exception)

 

Parent class : Throwable (be thrown)

There are two subclasses: Error (Error)        Exception (abnormal)

Error is the parent of all the wrong class, Exception is the superclass of all exception classes.

as the picture shows:

 

 

 

 

format:

Try{

     Block of code needs to be detected;

} catch ( Exception class variable name ) {

     Exception handling code;

}finally{

    Statement that must be executed block;

}

 

Try ..... catch ..... finally run sequence :

1, when the try when no abnormality block, try after the completion of block skip running catch block, the last run finally

2, when the try block abnormal time, try block to the exception in operation, a direct jump catch block, and finally the last run finally

3, if a try block and a plurality of catch blocks MS, when an abnormality occurs, only the implementation of the first and the exception object matching catch block, the rest of the catch block all ignored.

4 , in the try block and a catch block, there is return statement, the finally statement block will be executed before the method returns.

5 , when the virtual machine off, System.exit (0) ; the finally block does not execute.

 

Special case: the try corresponding to a plurality of catch , if there is the parent class statement block, must be on the bottom

 

 

 

 

 

Abnormal divided into:

 

1, compile abnormalities (abnormalities): Developers have to deal with, otherwise it will not compile (forced by the try ... the catch )

2, a runtime exception (unchecked exception): Developers can handle, you can not deal with, if not treated can compile. (Not mandatory, find errors to correct, without the try ... the catch )

 

Common abnormalities ( all runtime exceptions are RuntimeException subclasses ):

1ArithmeticException 算术异常,在执行数学运算时,进行了非法操作时抛出(除零)

2ArrayIndexOutofBoundsException 数组下标越界异常,在访问数组元素时,使用了非法的下标(负数,大于或等于数组长度)抛出。

3ClassCastException 类型转换异常,当大类型强转为小类型时,小类型的变量无法引用大类型变量所指向对象时抛出。

4NullPointerException 空指针异常,当对null值调用属性或方法时抛出。

5NumberFormatException 数字转换异常,将字符串转化为数字类型时,字符串不是合法的数字格式时抛出。

6Java.Util.regex.patter nSyntaException 正则表达式格式异常。当运用正则表达式验证时,正则表达式书写错误时抛出。

7Java.Io.FileNotFoundException 操作文件流时,目标文件不存在时抛出

8Java.io.NotSerializableException  在通过对象流传输对象时,对象没有实现序列化接口时抛出。

9Throw new Exception();创建异常对象,并抛出。

10Java.lang,ClassNotFoundException 加载类时,指定路径的类不存在时抛出。

 

 

Guess you like

Origin www.cnblogs.com/suger-4/p/11972214.html