Java exception

Time:2022-5-7

I abnormal

  • concept

    Abnormal conditions during the execution of the program will eventually lead to the abnormal stop of the JVM. The error is not an exception, but a problem out of the control of the programmer.

  • arrangement

    The exception class is the father of all exception classes.
    Java programs usually do not catch errors. Errors usually occur in the case of serious failures, which are outside the scope of Java program processing.
    Exception class and error are subclasses of throwable class:
    Error: serious errors, which cannot be handled, can only be avoided in advance, just like incurable diseases.
    Exception: exception. After the exception occurs, the programmer can correct it through code to make the program continue to run. It must be handled.

  • common method

    1.public void printStackTrace():The print error output stream is to print the details of the exception.
    2.public String getMessage():Return the detailed information about the exception that occurred, which is the reason for the error.

  • Anomaly classification

    Compile time exception: checked exception. At compile time, it will be checked. If the exception is not handled, the compilation fails.
    Run time exception: runtime exception. Runtime exceptions are exceptions that can be avoided by programmers and can be ignored at compile time.

  • Catch exception

    Use the try and catch keywords to catch exceptions. Try / catch code block to put possible abnormal code. They cannot be used alone.
    Syntax:

    try{    
        Write code that may cause exceptions
    }Catch (exception type E){
        Code to handle exceptions
    }

    demonstration:

    try{
        int i =1 /0;
    }catch(ArithmeticException e){
        System. out. Println ("brother, zero cannot be divisor");
    }
    System. out. Println ("passing");

    Run output results:

    Brother, zero cannot be divisor
    pass by
  • Multiple capture block

    The case where a try code block is followed by multiple catch code blocks is called multiple capture.
    Syntax:

    try{    
        Write code that may cause exceptions
    }Catch (exception type E1){
        Code to handle exceptions
    }Catch (exception type E2){
        Code to handle exceptions
    }Catch (exception type E3){
        Code to handle exceptions
    }

    Interpretation:
    1. You can add countless catch blocks after the try statement.
    2. If an exception occurs in the try code, the exception is thrown to the first catch block.
    3. If the data type of the exception thrown matches the similar exception type, it will be caught here.
    4. If it does not match, it will be passed to the second catch block.
    5. Do this until the exception is caught or passed through all catch blocks.

  • throw/throws

    If you need to throw an exception yourself in the program, that is, to inform the caller of the exception object, you should usethrowsentence
    Format:

    Throw new exception class name (parameter);

    You can use the throw statement to implement the following code:

    public class ThrowDemo {
     public static void main(String[] args) {
       int[] arr = {2,4,52,2};
       int index = 4;
       int element = getElement(arr, index);
       System.out.println(element);
       System. out. Println ("closure");
      }
      public static int getElement(int[] arr,int index){ 
      [judge whether the index is out of bounds]
       if(index<0 || index>arr.length-1){   
        [if the judgment conditions are met, the method cannot continue the operation. 
         The exception is notified to the caller. In this case, use the exception to solve it.]        
         Throw new ArrayIndexOutOfBoundsException ("out of bounds ~");
      }
       int element = arr[index];
       return element;
     }

    Indicates the type of exception that may be thrown when this method is executed. If a method does not catch a compile time exception, the method must usethrowsKeyword and let the caller handle it. Throws should be placed at the end of the method.
    Format:

    Modifier return value type method name (parameter) throws exception class name 1, exception class name 2... {}

    demonstration:

    public class ThrowsDemo {
     public static void main(String[] args) throws FileNotFoundException {
      read("a.txt");
    }
    
    [problems with this function will be reported to the caller, otherwise]
     public static void read(String path) throws FileNotFoundException {
      if (!path.equals("a.txt")) { 
         
          Throw new FileNotFoundException ("Muyou");
        }
       }
     }

    Note:
    If the method may have multiple exceptions, you can write multiple exception classes after throws, separated by commas.

  • finally

    1.finallyThe code block to be executed after the catch code block is created.
    2. No matter what happens or not, the code in the finally code block will always be executed.
    demonstration:

    try{
      int i =1 /0;
    }catch(ArithmeticException e){
      Throw new arithmeticexception ("brother, zero cannot be divisor");
    }finally{
      System. out. Println ("I don't care, I'll execute it.");
      }
      System. out. Println ("passing");

    Run output results:

    [error reporting exception] exception in thread "main" Java Lang. arithmeticexception: brother, zero cannot be a dividend
    I don't care. I'll do it.

    Note:
    Finally cannot be used alone.