Java动手动脑04

Java动手动脑04

计算时表达式除数为零:

源代码:

import javax.swing.*;

 

class AboutException {

   public static void main(String[] a)

   {

      int i=1, j=0, k;

      k=i/j;

 

 

try

{

 

k = i/j;    // Causes division-by-zero exception

//throw new Exception("Hello.Exception!");

}

 

catch ( ArithmeticException e)

{

System.out.println("0.  "+ e.getMessage());

}

 

catch (Exception e)

{

if (e instanceof ArithmeticException)

System.out.println("0");

else

{  

System.out.println(e.getMessage());

 

}

}

 

 

finally

     {

      JOptionPane.showConfirmDialog(null,"OK");

     }

 

  }

}

心得:

运行之后有了以下提示:

Exception in thread "main" java.lang.ArithmeticException: / by zero

at AboutException.main(AboutException.java:7)

但是我使用以下代码:

import javax.swing.*;

class AboutException {

   public static void main(String[] a)

   {

      int i=1, j=0, k;

      k=i/j;

  }

}

仍会给出上述的提示。

仔细读了一下代码发现是因为上述代码在try代码块之前就已经写出了分母为零的运算式,后面的对抛出的代码也就没有实现。删除那条语句之后便有了以下提示:

第一个catch

0除.  / by zero

还有一个输出框提示。

若把第一个catch注释掉第二个catch便会执行,则有一下提示:

第二个catch

0除

并有一个输出框提示。

try语句里面有问题并抛出了问题,若被一个catch接收了,则抛出的东西不会再被下一个catch接受,但是最后的finally语句块一直都会执行的。

CatchWho:

源代码:

public class CatchWho {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException();

             }

             catch(ArrayIndexOutOfBoundsException e) {

                System.out.println(  "ArrayIndexOutOfBoundsException" +  "/内层try-catch");

             }

 

            throw new ArithmeticException();

        }

        catch(ArithmeticException e) {

            System.out.println("发生ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

           System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch");

        }

    }

}

心得:

上述代码的运行测试结果为:

ArrayIndexOutOfBoundsException/内层try-catch

发生ArithmeticException

由此可得catch会优先与最近的try结合。

Catchwho2:

源代码:

public class CatchWho2 {

    public static void main(String[] args) {

        try {

             try {

                 throw new ArrayIndexOutOfBoundsException();

             }

             catch(ArithmeticException e) {

                 System.out.println( "发生内层ArithmeticException");

             }

            throw new ArithmeticException();

        }

        catch(ArithmeticException e) {

            System.out.println("发生外层ArithmeticException");

        }

        catch(ArrayIndexOutOfBoundsException e) {

            System.out.println( "ArrayIndexOutOfBoundsException");

        }

    }

}

运行测试结果:

ArrayIndexOutOfBoundsException

心得:

若抛出的错误没有被catch语句接住,同一个try代码块的接下来的语句也就不会再执行了,直接跳出这段代码块之后,被下一个合适的catch语句(外层的)接住。

EmbededFinally:

源代码:

public class EmbededFinally {

    

public static void main(String args[]) {

        

int result;

        

try {

            

System.out.println("in Level 1");

           

  try {

                

System.out.println("in Level 2");

  // result=100/0;  //Level 2

               

  try {

                   

  System.out.println("in Level 3");

                      

  result=100/0;  //Level 3

                

}

                

catch (Exception e) {

                    

System.out.println("Level 3:" + e.getClass().toString());

                

}

                

                

finally {

                    

System.out.println("In Level 3 finally");

                

}

                

               

// result=100/0;  //Level 2

            

}

            

catch (Exception e) {

               

  System.out.println("Level 2:" + e.getClass().toString());

           

  }

  finally {

                

System.out.println("In Level 2 finally");

           

 }

             

// result = 100 / 0;  //level 1

        

}

        

catch (Exception e) {

            

System.out.println("Level 1:" + e.getClass().toString());

        

}

        

finally {

           

  System.out.println("In Level 1 finally");

        

}

    

}

}

运行测试结果:

当异常在第三层try语句抛出的时候,finally语句的执行顺序是按照由第三层到第一层的顺序由内到外一步步执行的。

当异常在第二层被抛出时,finally语句是从第二层到第一层的顺序由内到外执行的。

当异常是在第一层的后面抛出的,由于finally无论对应的代码部分是否抛出异常都会执行,因此先是执行了第三层和第二层finally语句,而后执行第一层的catch语句,再执行第一层的finally语句。

Finally语句块是不是一定会执行:

源程序:

public class SystemExitAndFinally {

    

public static void main(String[] args)

    {

        

try{

            

System.out.println("in main");

            

throw new Exception("Exception is thrown in main");

             //System.exit(0);

        

}

        

catch(Exception e)

        {

System.out.println("catch语句执行!");

            

//System.out.println(e.getMessage());

            

System.exit(0);

        

}

        

finally

        

{

            System.out.println("finally语句执行!");

//System.out.println("in finally");

        

}

    

}

}

运行测试结果:

当在catch语句中加入System.exit(0);语句时,运行结果为:

in main

catch语句执行!

即是后面的finally语句并没有被执行。

catch语句中没有上述语句时,finally语句自然会正常执行。

而当在try语句中出现上述语句则会报错,需要删去这条语句。

心得:

之前的代码无论是否抛出错误,后面的finally语句一般情况下都是会执行的。但是当在catch语句中加上System.exit(0);之后,程序便不会执行后面的内容了,finally语句自然也不会执行了。

PrintExceptionStack:

源代码:

// UsingExceptions.java

// Demonstrating the getMessage and printStackTrace

// methods inherited into all exception classes.

public class PrintExceptionStack {

   public static void main( String args[] )

   {

      try {

         method1();

      }

      catch ( Exception e ) {

         System.err.println( e.getMessage() + "\n" );

         e.printStackTrace();

      }

   }

   public static void method1() throws Exception

   {

      method2();

   }

   public static void method2() throws Exception

   {

      method3();

   }

   public static void method3() throws Exception

   {

      throw new Exception( "method3" );

   }

}

运行测试结果:

method3

java.lang.Exception: method3

at PrintExceptionStack.method3(PrintExceptionStack.java:28)

at PrintExceptionStack.method2(PrintExceptionStack.java:23)

at PrintExceptionStack.method1(PrintExceptionStack.java:18)

at PrintExceptionStack.main(PrintExceptionStack.java:8)

心得:

由此可得抛出的错误是按照栈的储存方式储存的,即先进后出,因此的得到了上述的输出结果。

猜你喜欢

转载自www.cnblogs.com/ruangongyouxi/p/9941407.html