1. Concept of exception
(1) What is an exception
The so-called exception refers to some abnormal things that occur during the operation of the program (for example, except 0 overflow, the array subscript is out of bounds, and the file to be read does not exist)
(2) Consequences caused by abnormality
If an exception event occurs during the execution of a java program, an exception class object can be generated. The exception class object encapsulates the information of the exception event and submits it to the Java runtime system. This process is called throwing an exception. If it is not handled, it will directly lead to the direct interruption of the program.
(3) How to prevent program interruption
A well-designed program should provide methods to deal with program exceptions when they occur, so that the program will not be blocked or produce unpredictable results because of the occurrence of exceptions.
2. Java exception handling mechanism
The try {} statement block contains the Java code to be detected, which may throw exceptions or execute normally
Catch (exception type) {} block is a catch block that can handle the exception when the Java runtime system receives the exception object thrown in the try block (there can be multiple catch blocks)
finally {} no matter whether the system throws an exception or not, it will be executed. It is generally used to release resources. In addition to executing the system exit(0)
Code example 1:
class person{ String name; } public class Test { public static void main(String[] args) { person p = null; try { p.name = "zheng"; }finally{ //Whether the system throws an exception or not, it will execute it System.out.println("end"); } } }
Code example 2:
class person{ String name; } public class Test { public static void main(String[] args) { person p = null; try { p.name = "zheng"; }catch(java.lang.NullPointerException e){ //Capture and handle exceptions; java.lang.NullPointerException: exception type //catch(Exception) {}: handle all exceptions System.out.println("Null pointer, check whether it is not initialized"); } finally{ System.out.println("end"); } } }
3. throw and throws
Throw is used to throw an exception manually. As a programmer, you can throw exceptions manually at any position
throws is used to identify the exception to expose on the method. The exception thrown is handled by the caller
Differences between the two:
throw is used in the method, followed by the exception class object * * (thrown manually)**
throws is decorated on the method to tell the caller that the method may throw an exception, followed by the class name * * (Declaration) of the exception that may be thrown**
Code example:
class bar{ int age; public bar(int age){ this.age = age; } public void check() throws IllegalArgumentException{ if (age < 18){ throw new IllegalArgumentException("Too young"); //Throw exception } } } public class Test { public static void main(String[] args) { bar b = new bar(15); try{ b.check(); }catch(IllegalArgumentException e){ System.out.println(e.getMessage()); } System.out.println("end"); } }
Common exceptions: RuntimeException, IOException, SQLException, ClassNotFoundException
Custom exceptions: the exception system provided by Java cannot foresee all errors that you want to report
Custom exception classes must inherit from existing exception classes
The easiest way to create a new exception type is to let the compiler generate a default constructor
The most important part of an exception is its class name
You can define a construction method that accepts string parameters for exceptions. String parameters describe exception information
Custom exception example code:
class bar{ int age; public bar(int age){ this.age = age; } public void check() throws AgeLessThanEighting{ if (age < 18){ throw new AgeLessThanEighting("Too young");//Throw custom exception } } } class AgeLessThanEighting extends Exception{ //Custom exception String massege; public AgeLessThanEighting(String massege){ this.massege = massege; } } public class Test { public static void main(String[] args) { bar b = new bar(15); try{ b.check(); }catch(AgeLessThanEighting e){ System.out.println(e.massege); } System.out.println("end"); } }