1, Cognitive abnormality
Exceptions are divided into runtime exceptions and compile time exceptions
1. Abnormal operation
Exception occurred during program operation, runtime exception (non checked exception)
public static void main(String[] args) { System.out.println(10/0); //Arithmetic anomaly int[] array = {1,2,3,4}; System.out.println(array[100]); //Array out of bounds exception }
2. Compile time exception
The error occurred during the compilation of the program, and the compilation time exception (checked exception)
Because the idea is compiled automatically, a red underline will be reported during compilation, indicating a compilation error.
throws CloneNotSupportedException
A declaration is required to solve the above problems.
2, Defensive programming
1.LBYL
Look Before You Leap
2.EAFP (used in java)
"It's easier to get forgiveness afterwards than to get permission in advance", that is, operate first and deal with problems later. ( It’s Easier to Ask Forgiveness than Permission)
3, try() catch ()
This method is EAFP.
public static void main(String[] args) { int a = 10; int b = 0; System.out.println(a/b); System.out.println("hello word"); }
In the above code, an arithmetic exception will occur, and the exception will be handed over to the JVM to handle the exception. Once the exception is handed over to the JVM, the program will terminate abnormally and will not continue to execute future code.
public static void main(String[] args) { int a = 10; int b = 0; String str = null; try { //Possible exception codes System.out.println(str.lenth()); System.out.println(a/b); }catch (ArithmeticException arithmeticException){//Catch exception and add a parameter arithmeticException.printStackTrace(); //Use the printStackTrace of this parameter to output this exception. System.out.println("Arithmetic exception occurred"); }catch (NullPointerException c){ //Catch multiple exceptions c.printStackTrace(); } System.out.println("hello word"); }
If you handle the exception yourself, you will continue to execute the program without terminating the program.
The above code can also be modified to capture multiple exceptions
public static void main(String[] args) { int a = 10; int b = 0; String str = null; try { System.out.println(a/b); System.out.println(str.length()); }catch (ArithmeticException | NullPointerException arithmeticException){ arithmeticException.printStackTrace(); System.out.println("An arithmetic exception or null pointer exception occurred"); } System.out.println("hello word"); }
Use a catch to catch all exceptions
In the figure: error represents a program error (stack overflow error, etc.)
Exception is an exception, which is divided into run-time exception (red part) and compile time exception (blue part)
So to catch all exceptions, you can use Exception
try { System.out.println(a/b); System.out.println(str.length()); }catch (Exception e){ e.printStackTrace(); System.out.println("An arithmetic exception or null pointer exception occurred"); }
If you continue to write catch after Exception, an error will be reported
try { System.out.println(a/b); System.out.println(str.length()); }catch (Exception e){ e.printStackTrace(); System.out.println("An arithmetic exception or null pointer exception occurred"); }catch(ArithmeticException){ //Make an error report. If you put it in the front, you won't make an error report }
Don't put parent exceptions at the top.
4, finally
Use finally to release some corresponding resources
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.println(10/0); }catch (ArithmeticException e){ e.printStackTrace(); System.out.println("Arithmetic exception occurred"); }finally { scanner.close(); } }
Optimized code: you can put the resources to be released in parentheses.
public static void main(String[] args) { try (Scanner scanner = new Scanner(System.in)) { System.out.println(10 / 0); } catch (ArithmeticException e) { e.printStackTrace(); System.out.println("Arithmetic exception occurred"); } }
finally, precautions
finally, the execution order is after try catch
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); try { System.out.println(10/0); return 10; }catch (ArithmeticException e){ e.printStackTrace(); System.out.println("Arithmetic exception occurred"); }finally { return 20; //Do not return in finally } }
When you run the code, the value returned is 20
Note: finally must be executed at the end.
5, Exception handling
Exception handling will be passed upward to find appropriate methods for handling
public static void func() { System.out.println(10/0);//There is no exception handling here } public static void main(String[] args) { try{ func(); //Exception handling is performed here }catch (ArithmeticException e){ System.out.println("Arithmetic exception occurred"); } }
If there is no suitable method to handle the exception after passing it all the time, it will eventually be handed over to the JVM for processing, and the program will terminate abnormally
6, Throw exception
We can throw exceptions manually
// public static void func(int a,int b) throws ArithmeticException{ //The declaration may throw an exception that needs to be handled by itself. if(b == 0){ throw new ArithmeticException("The parameter is 0"); //Throw an exception manually } System.out.println(10/0);//There is no exception handling here } public static void main(String[] args) { try{ func(); }catch (ArithmeticException e){ System.out.println("Arithmetic exception occurred"); } }
7, Custom exception
class MyException extends RuntimeException{ //Custom exception class public MyException(String a){ //Constructor super(a); } } public class testDome { public static void main(String[] args) { try{ int i = 10; if (i == 10){ throw new MyException("i=10"); //Use a constructor with a constructor } }catch(MyException a){ a.printStackTrace(); System.out.println("An exception was caught"); } }