java abnormal nested and cascaded

A separate capture or nested

Let's take a look at the following code:

public  class Cal { 

    public  int div ( int A, int B) {
         int Result = A / B;
         return Result; 
    } 

    public  static  void main (String [] args) { 
        Scanner Scanner = new new Scanner (the System.in);
         int S 0 = ; 

        int num1 = 0 ;
         int num2 = 0 ; 

        // 1, where might throw an exception 
        System.out.print ( "num1 =" ); 
        num1 = scanner.nextInt (); 
        System.out.print ("num2 =" ); 
        num2 = scanner.nextInt (); 

        Cal CAL = new new Cal ();
         // 2, there may also be thrown 
        S = cal.div (num1, num2); 

        System.out.println (S ); 
    } 
}

 

There are in this code may throw an exception there are two places, so how should we deal with it.

1, of course, we can capture separately. The following code:

public  class Cal { 

    public  int div ( int A, int B) {
         int Result = A / B;
         return Result; 
    } 

    public  static  void main (String [] args) { 
        Scanner Scanner = new new Scanner (the System.in);
         int S 0 = ; 

        int num1 = 0 ;
         int num2 = 0 ;
               // . 1, this may throw exceptions 
        the try { 
            
            of System.out.print ( "num1 ="  );
            num1= scanner.nextInt();
            System.out.print("num2=");
            num2 = scanner.nextInt();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Cal cal = new Cal();
                //2、这里也可能抛出异常
        try {
            s = cal.div(num1, num2);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println(s);
    }
}

 

2, we can also use nested inside the try try statement. As shown in the following code:

public  class Cal { 

    public  int div ( int A, int B) {
         int Result = A / B;
         return Result; 
    } 

    public  static  void main (String [] args) { 
        Scanner Scanner = new new Scanner (the System.in);
         int S 0 = ; 

        int num1 = 0 ;
         int num2 = 0 ; 

        the try {
             // . 1, this may throw exceptions 
            of System.out.print ( "num1 =" ); 
            num1= scanner.nextInt();
            System.out.print("num2=");
            num2 = scanner.nextInt();
            
            try {
                Cal cal = new Cal();
                //2、这里也可能抛出异常
                s = cal.div(num1, num2);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        

        System.out.println(s);
    }
}

 

Second, using a cascade catch the exception

The two methods described above is not a good way, that excessive use efficiency try to catch exceptions will affect the program. Therefore, we recommend using a cascade catch exceptions. The following format

try{

…...

}catch(ArrayIndexOutOfBoundsException e) {

……

} catch(ArithmeticException e) {

……

} catch(Exception e) {

……

}

 

Note: When using multiple catch statements, exception classes must be located before the exception parent.

So this is the wrong way.

try{

…...

} catch(Exception e) {

……

} catch(ArrayIndexOutOfBoundsException e) {

……

}

 

Well, then we can modify the above code as follows:

public  class Cal {
     public  int div ( int A, int B) {
         int Result = A / B;
         return Result; 
    } 
    public  static  void main (String [] args) { 
        Scanner Scanner = new new Scanner (the System.in);
         int S 0 = ; 

        int num1 = 0 ;
         int num2 = 0 ;
         the try {
             // . 1, this may throw exceptions 
            of System.out.print ( "num1 =" ); 
            num1= scanner.nextInt();
            System.out.print("num2=");
            num2 = scanner.nextInt();

            Cal cal = new Cal();
            //2、这里也可能抛出异常
            s = cal.div(num1, num2);
        } catch (ArithmeticException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (InputMismatchException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }
}

 

Due to multiple or affect the efficiency of the use try. So if we run into circulation, it should try to put the statement out of the loop, for example, we do not recommend you to write code like this:

public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4 };
        Cal cal = new Cal();

        for (int i = 0; i < arr.length; i++) {
            try {
                int s = cal.div(arr[i], 2);
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
    }

 

You can modify become such:

    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4 };
        Cal cal = new Cal();
        try {
            for (int i = 0; i < arr.length; i++) {
                int s = cal.div(arr[i], 2);
            }
        } catch (Exception e) {
            // TODO: handle exception
        }
    }

 

Guess you like

Origin www.cnblogs.com/weibanggang/p/11184697.html