On Java exception handling

Keywords: Java

1, Exit exit exception:

import java.util.Scanner;


public class Test3exit {

    /**
     * @param Cats in Fangshan
     * finally When not to leave
     * alt+shift+z
     */
    public static void main(String[] args) {
        try {
            Scanner input=new Scanner(System.in);
            System.out.print("Please enter the first integer:");
            int num1=input.nextInt();//1)Input is not of integer type 2) division input 0
            System.out.print("Please enter a second integer:");
            int num2=input.nextInt();
            int jg=num1/num2;//input num2 Possible input 0
            System.out.println(jg);
        } catch (Exception e) {
            System.out.println("Abnormal");
           System.exit(1);//Sign out java Virtual machine
            e.printStackTrace();
        }finally{
            System.out.println("finally Sentence");
        }

    }

}

 

2, return returns:

"If return is in the exception handling mechanism block, the execution flow of the finally block is higher than return; however, in the method with return value, return returns the value of the current location, which is not affected by the finally block."

Please refer to xxx for detailed explanation

import java.util.Scanner;


public class Test4return {

    /**
     * @param Cats in the house
     * finally When not to leave
     * alt+shift+z
     */
    public static void main(String[] args) {
        try {
            Scanner input=new Scanner(System.in);
            System.out.print("Please enter the first integer:");
            int num1=input.nextInt();//1)Input is not of integer type 2) division input 0
            System.out.print("Please enter a second integer:");
            int num2=input.nextInt();
            int jg=num1/num2;//input num2 Possible input 0
            System.out.println(jg);
        } catch (Exception e) {
            System.out.println("Abnormal");
            return;//Program return
            
        }finally{
            System.out.println("finally Sentence");
        }

    }

}

  

Many people think that this program will return and will not go finally. In fact, it is not; the following is the implementation process:

 

 

----------------------------------------------------------------------------

|Summary: System.exit(); / / the virtual machine exiting java is the only case where the finally block does not execute|

----------------------------------------------------------------------------

3, Add:

Normal exit should use return 0; 0 means no error
exit the application with exception. The return value is given to the compiler for other corresponding operations

In the main () function, exit is exactly the same as return. But in other functions, one is to give control to the compiler to exit the program, and the other is to return to the main function Calling location

 

 

 

 

(C) The cat in the room. All rights reserved.
 https://www.cnblogs.com/lsy131479/

 

If you need to reprint, please indicate the source!!!

Posted by simple_man_11 on Fri, 03 Apr 2020 18:32:44 -0700