Day06 Exception try catch finally return execution sequence throw and throws Error and Exception

Day06 exception

  • Learn exception handling, understand the inheritance diagram of exceptions, customize exceptions, and practice more

    • Exception: Throwable is the parent class of the top-level exception. It has two subclasses, exception and Error

      • Error: an error that the program cannot handle. Once the program is forced to stop running, such as memory overflow OOM

      • Exception: an exception that can be handled without causing the program to stop. It is also divided into runtime exception RunTimeException and compile time exception CheckException

        • CheckException: compile time exception. Syntax problems will be displayed during compilation

        • RunTimeException: occurs when the program is running. It will lead to h

        • Cause execution failure of current thread

          • Arithmetic Exception arithmetic error exception
          • ArrayIndexOutOfBounds Exception array subscript out of bounds
          • NullPoint Exception null pointer exception
          • ClassNotFound Exception class cannot find exception
          • IllegalArgument Exception illegal parameter exception
          • ClassCast Exception object cast exception
          • Numerformart exception number conversion format exception
          • ...
        • Non checking exception (screenshot from rookie tutorial)

        • Check for sexual exceptions (screenshot from rookie tutorial)

    • Exception handling: throw throws and try catch finally

      • try{possible exception code}catch{catch exception}

        try {//Divide by zero exception
            int i = 1/0;
        }catch (Exception e){
            System.out.println(e);
        }
        try {//subscript out of bounds
            int[] arr = new int[5];
            arr[5]=5;
        }catch (Exception e){
            System.out.println(e);
        }
        

      • try{} catch{} finally{}

        1. Both the try code block and the finally code block have returns. The last return is preferred. If there is no return in finally, the return in try is executed

          • Whether or not there is an exception in try finally, try finally has a return that returns finally when called
          • If there is no exception in try, try has a return. Finally, a return without a return try will be executed
          • When a try has an exception, a try with a return finally without a return try will not be executed. An external return will be executed
          public class TestException02 {
              public  static String testReturn01(){
                  //Whether or not there is an exception in try finally, try finally has a return that returns finally when called
                  try{
                      //int i = 1/0;
                      return "try";
                  }catch (Exception e){
                      System.out.println(e);
                  }finally {
                      return "finally";
                  }
              }
              public  static String testReturn02(){
                  // If there is no exception in try, try has a return. Finally, a return without a return try will be executed
                  try{
                      return "try";
                  }catch (Exception e){
                      System.out.println(e);
                  }finally {
                      System.out.println("finally None in return");
                  }
                  return "what";
              }
              public  static String testReturn03(){
                  // When a try has an exception, a try with a return finally without a return try will not be executed. An external return will be executed
                  try{
                      int i = 3/0;
                      return "try";
                  }catch (Exception e){
                      System.out.println(e);
                  }finally {
                      System.out.println("finally None in return");
                  }
                  return "what";
              }
          
              public static void main(String[] args) {
                  System.out.println(TestException02.testReturn01());
                  System.out.println(TestException02.testReturn02());
                  System.out.println(TestException02.testReturn03());
              }
          }
          

        2. finally{} code block does not execute

          • System Exit (0) terminate jvm
      • throw manual exception and throws upward exception

        throwthrows
        Method body throws the specified exception object nameClass name of exception thrown after method declaration
        Exceptions are handled by statements in the method body (handled by itself)Throw an exception up (handled by the method caller)
        Throw a specific exception, and the exception occurs when you execute throwthrows indicates that possible exceptions are thrown
        Throw can only throw one exception class namethrows can throw multiple exception class names, separated by
        public class TestThrows {
            public static void main(String[] args) throws ArithmeticException{//throws follows the method with the exception class name
                int a = 10;
                int b = 0;
                if (b == 0){//throw is used for the exception object name inside the method
                    throw new ArithmeticException();
                }else {
                    System.out.println(a/b);
                }
            }
        }
        
  • Common exceptions: RuntimeException, NullpointException, FileNotFoundException, etc

    • Simulate some common exceptions:

      • Arithmetic Exception arithmetic error exception

        //Arithmetic anomaly
        int i = 1/0;
        

      • ArrayIndexOutOfBounds Exception array subscript out of bounds

        int[] arr = new int[2];
        arr[2] = 3;
        

      • NullPoint Exception null pointer exception

        String s = null;
        if(s.length()>0){
            System.out.println("hello");
        }
        

      • ClassCast Exception object cast exception

        Object m = new String();
        System.out.println((Integer)m);
        

        Summary:

        public class Test003 {
            public static void main(String[] args) {
                    //Arithmetic anomaly
                    //int i = 1/0;
                    //subscript out of bounds
                    /*int[] arr = new int[2];
                    arr[2] = 3;*/
                    //Null pointer exception
                    /*String s = null;
                    if(s.length()>0){
                        System.out.println("hello");
                    }*/
                    //Object cast exception
                    /*Object m = new String();
                    System.out.println((Integer)m);*/
            }
        }
        
  • Distinguish between Error and Exception

  • Abnormality is the most important! Practice more

Tags: Java programming language

Posted by tomtimms on Wed, 01 Jun 2022 18:04:36 +0530