1, Abnormal
- Exceptions refer to unexpected conditions during program operation, such as file missing, network connection failure, illegal parameters, etc.
- The exception occurs during the program running, which affects the normal program execution process.
Simple classification:
- Checking exceptions: exceptions caused by user errors or problems that cannot be foreseen by programmers. For example, to open a file that does not exist, an exception occurs.
- Runtime exception: an exception that occurs when the program is running. It is ignored when the program is compiled (no error is prompted when programming in the IDE). An exception occurs when the program is running.
- Error: the error is not an exception, but a problem out of the control of the programmer. For example, when the stack overflows, an error occurs and cannot be checked at compile time.
Exception architecture:
- Java treats exceptions as objects and defines a base class java Lang. throwable is the superclass of all exceptions.
- Exception classes are divided into two categories: Error and exception.
Error:
- Objects are generated and thrown by the Java virtual machine, most of which are independent of the operations performed by the coder
- Java virtual machine runs incorrectly. Java virtual machine usually chooses to terminate
Exception:
- Exceptions are usually caused by program logic errors. Programmers should avoid such exceptions as much as possible from a logical point of view
- Error is usually a catastrophic error that cannot be controlled and handled by the program
- Exception s can usually be handled by the program, and these exceptions should be handled as much as possible in the program
2, Exception handling mechanism
- Throw exception
- Catch exception
- Exception handling Keywords: try, catch, finally, throw, throws
-
try-catch-finally
public class Test { public static void main(String[] args) { int a = 1; int b = 0; //ctrl+alt+T -->try-catch-finally // System.out.println(a/b); //java.lang.ArithmeticException: / by zero try { //Monitoring area System.out.println(a/b); } catch (ArithmeticException arithmeticException) { //Catch can catch multiple exceptions. The exception level should be from small to large, and only the most consistent exception (small) is executed System.out.println("catch arithmeticException"); //catch arithmeticException } catch(Exception exception){ System.out.println("catch exception"); //Do not execute } catch(Throwable throwable){ System.out.println("catch throwable"); //Do not execute } finally { //Optional System.out.println("finally"); //finally } } }
-
throw
public class Test2 { public static void main(String[] args) { int a = 1; int b = 0; //There is no exception code (a/b), but an exception occurred new Test2().test(a,b); //java.lang.ArithmeticException } public void test(int num1,int num2){ if (num2 == 0){ throw new ArithmeticException(); //Actively throw exception } } }
-
throws
public class Test3 { public static void main(String[] args) { int a = 1; int b = 0; try { new Test3().test(a,b); } catch (Exception ArithmeticException) { System.out.println("Handling exceptions"); //Handling exceptions } } //Assuming that this exception cannot be handled in this method, an exception is thrown on the method and handed over to the outside for processing public void test(int num1,int num2) throws ArithmeticException{ System.out.println(num1/num2); } }
3, Custom exception
Using Java's built-in exceptions can describe most of the exceptions that occur during programming. In addition, users can also customize exceptions. Users can customize exceptions by inheriting the Exception class
Custom exception class:
- Create custom exception class
- Throw an exception object through throw in a method
- Handle the exception in the method that throws the exception, otherwise throw the exception through throws at the declaration of the method
- Catch and handle exceptions where exception method calls occur
Custom exception class
public class MyException extends Exception{ //Return "MyException {" + "detail=" + detail + '}' private int detail; //Parametric structure public MyException(int a) { this.detail = a; } //toString: abnormal print information @Override public String toString() { return "MyException {" + "detail=" + detail + '}'; } }
Main test class
public class Test { public static void main(String[] args) { try { test(11); //Parameters passed: 11 } catch (MyException e) { System.out.println("Exception handling:"+e); //Exception handling: MyException {detail=11} } } //Possible exception methods public static void test(int a) throws MyException{ System.out.println("Parameters passed:"+a); if (a>10){ throw new MyException(a); } System.out.println("ok"); } }
Practical experience summary
- When dealing with runtime exceptions, logic can be used to reasonably avoid and assist in try catch processing
- After multi-layer catch, add a catch (Exception) to handle exceptions that may be missed
- For uncertain code, you can also add try catch to handle potential exceptions
- Try to handle exceptions
- Try to use the finally statement block to release the occupied resources