Exception: a problem that prevents the current method or scope from continuing execution — Think in Java
When an exception occurs and the method terminates execution, we need to handle the exception in order for the program to run properly
The exception handling mechanism in Java, in fact, is when the code has a logical error, as far as possible to return error information, to help us locate the error location
Anomaly architecture
Throwable
Is the highest parent of the entire exception handling mechanism, which has two subclassesError
和 Exception
Error: A serious Error that the program cannot handle. We do not handle it
- This error is generally irrelevant to the operator, and the developer and application have no ability to solve the problem, so the JVM usually terminates the thread
Exceptions: Exceptions can be classified as run-time exceptions and compile time exceptions (non-run-time exceptions)
-
RuntimeException: Passed at compile time, failed at run time
-
OtherException: During compilation, the compiler detects that something may be wrong with a piece of code, requiring the programmer to make a solution to the error in advance. Otherwise the compilation will not pass
In this case, the error in FileReader is compile-time exception, we need to deal with the possible exception in advance
The following code will have a runtime exception. During compilation, it will be fine, but the running code will report an error
Int arr [] = {1, 2, 3}; System.out.println(arr[4]);Copy the code
Handling exceptions
Java’s default handling of exceptions: throw the problem to the next level up
When an exception occurs, Java creates an instance object based on the exception class in question and throws that object to the next level up
public static void main(String[] args) {
method();
}
public static void method(){
System.out.println(10/0);
}
Copy the code
The following error message appears when running the code
When you execute a method inside a method, you get an exception condition, create a ArithmeticException object, throw it to the level above the method that called it, which is method, which doesn’t handle exceptions, throw it to the level above the main method, It is then thrown to the JVM, which gets the problem and prints the location and cause of the error.
-
The problem can be handled by itself: try catch handling does not affect subsequent code execution
-
Issues cannot be handled by themselves: throws processing
try catch:
Int arr [] = {1, 2, 3}; System.out.println(arr[4]); System.out.println(" Subsequent code execution......") );Copy the code
Code that encounters an exception will not execute subsequent code, and will directly report an error
Try {int arr [] = {1, 2, 3}; System.out.println(arr[4]); } the catch (ArrayIndexOutOfBoundsException e) {System. Out. Println (" an array "); } system.out.println (" Subsequent code execution......") );Copy the code
Use a try catch to handle the code and then the code executes normally
throws:
// Define the people attribute: age and its set get method people p = new people (); p.setAge(-20); System.out.println(p.getAge());Copy the code
An age of -20 is obviously not reasonable to modify the SET method
public void setAge(int age) { if(age>=0){ this.age = age; } else {system.out.println (" age error "); }}Copy the code
The default value 0 is obtained by the get method, and the occurrence of the default value 0 is also unreasonable
Let’s say we wrote people, someone else calls people, and the caller enters a negative age, which is out of our control, and we need to throw an exception
throw
Throws the exception object to the caller
throws
Declare a method by informing the caller that there is an exception to the method
public void setAge(int age) throws Exception { if(age>=0){ this.age = age; } else {throw new Exception(" age error "); }}Copy the code
In addition, we can customize the Exception class by inheriting from Exception