Learn from teacher Wang Abnormal (c) The inheritance system of abnormal class

Source: Internet
Author: User

Inheritance system of Exception class Speaker: Wang Shaohua QQ Group No.: 483773664
Learning Goals:

1. Mastering the Abnormal system

2. Mastering the common methods of handling abnormal class

First, exception class inheritance system diagram

Java provides a rich class of exceptions that have strict inheritance relationships, as shown in

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/7F/B5/wKiom1cprILiJFkoAABOc9WBMDw295.png " data_ue_src= "E:\My knowledge\temp\dd76992d-73ea-46d0-b78d-c9ecf66db949.png" >

As you can see, Java divides all non-normal situations into two kinds, one is an exception (Exception) and the other is error, and they all inherit the Throwable parent class.

Second, Error

Error errors, which are generally referred to as virtual machine related issues. That is, only critical errors that cannot be recovered by the program itself. such as system crashes, virtual machine errors, dynamic link failures, and so on. This error is usually not handled by the application. Because the application should not attempt to catch this exception by using a catch block.

Third, Exception

A non-critical error thrown and handled by a Java application, which is divided into run-time exceptions and non-runtime exceptions.

Run-time exceptions, including RuntimeException and all of its subclasses, do not require the program to process them, that is, the program can compile and run even if it is handled in a program that does not make try-catch or other exception handling such as throws.

Non-running exception, except for run-time exceptions, other exception classes inherited by exception. It is characterized by the exception in the program must be processed, otherwise there will be a compilation error

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/7F/B5/wKiom1cprIKz4acFAAA3FjT-2-U899.png " data_ue_src= "E:\My knowledge\temp\e8f1d8bc-40df-4072-9407-e584e97cf2a5.png" >

Four, several common compile-time exceptions
12345678910111213141516171819 public class TestRunTimeException {    public static void main(String[] args) {        String paras[] = {};        try {            int a = Integer.parseInt(paras[0]);            int b = Integer.parseInt(paras[1]);            int c = a/b;            System.out.println("你输入的两个数相除的结果是:"+c);        } catch (IndexOutOfBoundsException e) {             System.err.println("数组越界:运行时程序输入的参数个数不够");        }catch (NumberFormatException e) {            System.err.println("数字格式异常:程序只能接受整数参数");        }catch (ArithmeticException e) {            System.err.println("算术异常:除数不能为零");        }catch (Exception e) {            System.err.println("未知异常");        }    }}
    • If the array is not long enough to run the program, an array out-of-bounds exception will occur: Indexoutofboundsexception.java will call the indexoutofboundsexception corresponding catch block to handle the exception

    • If the value in the array is not a number but a letter when you run the program, a number format exception occurs: Numberformatexception.java will call the numberformatexception corresponding catch block to handle the exception

    • If the divisor is 0 when you run the program, 0 exceptions occur: ArithmeticException. Java will call the arithmeticexception corresponding catch block to handle the exception

    • If you run the program with another exception that always exception an instance of the class or its subclasses, Java calls the exception corresponding catch block to handle the exception

Note: These exceptions are common exceptions, and you must understand the conditions in which these anomalies occur. There are also some common exceptions: such as null pointer anomalies (nullpointexception), and so on, these we later learn and work in the process, slowly familiar with and accumulated.

V. Access to exception information

If the program needs to access information about the exception object in the catch block, it can be obtained by calling the method of the exception parameter after the catch.

All exception objects contain the following common methods:

    • GetMessage (): String that returns a detailed description of the exception

    • Printstacktrace (): The trace stack information for the exception is output to the console

    • Printstackmessage (PrintStream s): Outputs the trace stack information for this exception to the specified output stream

    • Getstacktrace (): Returns the trace stack information for this exception.

12345678910 public class AccessExceptionMsg {    public static void main(String[] args) {        try {            FileInputStream fiStream = new FileInputStream("a.txt");        } catch (IOException e) {            System.out.println(e.getMessage());            e.printStackTrace();        }    }}

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/7F/B3/wKioL1cprVfzXgRlAAA_foR4wfM723.png " data_ue_src= "E:\My knowledge\temp\feba629b-8195-4b50-9222-8c739790da83.png" >

As can be seen from the above figure, the detailed description of the exception is: A.txt (the system cannot find the file specified.) )。 The following more detailed information is the exception trace information.












From for notes (Wiz)

Learn from teacher Wang Abnormal (c) The inheritance system of abnormal class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.