Dig into the various exception handling methods in Java

  • 2020-04-01 04:07:38
  • OfStack

1. Debug the tracking code:


  public static void enterTryMethod() { 
    System.out.println("enter after try field"); 
  } 
   
  public static void enterExceptionMethod() { 
    System.out.println("enter catch field"); 
  } 
   
  public static void enterFinallyMethod() { 
    System.out.println("enter finally method"); 
  } 

2. Throw Exception, no finally, when catch meets return

     


public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 0; //Throws an Exception and subsequent processing is rejected
      enterTryMethod(); 
      return res; //Exception has been thrown and there is no chance of execution
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1;  //Exception is thrown, giving you the opportunity to call the method and return the method value
    } 
  } 

Background output results:


  enter catch field 
  1 

3. Throws an Exception. When there is a return in the catch body, the code block in the finally body will be executed before the catch executes the return

     


public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 0; //Throws an Exception and subsequent processing is rejected
      enterTryMethod(); 
      return res; //Exception has been thrown and there is no chance of execution
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1;  //Exception is thrown, giving you the opportunity to call the method and return the method value
    } finally { 
      enterFinallyMethod(); //Exception is thrown, and the finally code will be executed before catch executes the return
    } 
  } 

Background output results:


  enter catch field 
  enter finally method 
  1 

4. Do not throw an Exception, and when a return is encountered in the finally block, the entire method ends when the finally completes


  public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 2; //Don't throw the Exception
      enterTryMethod(); 
      return res; //Get the opportunity to be executed, but the execution needs to be performed after the finally execution has completed
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1; 
    } finally { 
      enterFinallyMethod(); 
      return 1000; //In finally, there is a return statement that terminates the method and does not jump back to try or catch to continue execution after execution
    } 
  } 

Background output results:


  enter after try field 
  enter finally method 
  1000 

5. Do not throw an Exception. When the System. Exit () method is encountered ina block of finally code, it will end and terminate the entire program, not just the method

 


  public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 2; //Don't throw the Exception
      enterTryMethod(); 
      return res; //Gets a chance to be executed, but because the finally has terminated the program, the return value has no chance to be returned
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1; 
    } finally { 
      enterFinallyMethod(); 
      System.exit(0); //Finally contains the system.exit () statement, and system.exit () will exit the entire program and the program will be terminated
    } 
  } 

Background output results:


  enter after try field 
  enter finally method 

6. Throws Exception, when catch and finally both meet return, the return value of catch will not be returned, and the return statement of finally will terminate the entire method and return

     


 public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 0; //An Exception is thrown and subsequent processing is rejected
      enterTryMethod(); 
      return res; //Exception has been thrown and there is no chance of execution
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1; //The Exception has been thrown, getting a chance to be executed, but the return operation will be truncated by the finally
    } finally { 
      enterFinallyMethod(); 
      return 10; //Return ends the entire method with a return value of 10
    } 
  } 

Background output results:


  enter catch field 
  enter finally method 
  10 

7. No Exception is thrown, when finally meets return, the return value of the try will not be returned, and the return statement of finally will terminate the entire method and return

     


 public static int catchTest() { 
    int res = 0; 
     
    try { 
      res = 10 / 2; //Don't throw the Exception
      enterTryMethod(); 
      return res; //Gets an execution opportunity, but the return is truncated by a finally
    } catch (Exception e) { 
      enterExceptionMethod(); 
      return 1; 
    } finally { 
      enterFinallyMethod(); 
      return 10; //Return ends the entire method with a return value of 10
    } 
  } 

Background output results:


  enter after try field 
  enter finally method 
  10 


conclusion
In Java exception handling, the method does not end immediately after the program executes a block of code in the try, but instead continues to try to find out if the method has a block of code in the finally

      If there is no finally code block, the entire method completes the try code block by returning the corresponding value
      If there is a finally block, the program does not immediately execute the return when it reaches the return line in the try block, but first executes the code in the finally block

If there is no return in the finally block or no code to terminate the program, the program executes the finally block of code and then returns the try block to execute the return statement to terminate the entire method. If the finally block has a return in it or contains code that can terminate the program, the method will be terminated after the finally execution and will not jump back to the try block to execute the return
In the case of an exception, the principle is the same as above, you can replace the above "try" with "catch" to understand it.


Related articles: