Java的动手动脑(六)

时间:2023-03-09 08:19:16
Java的动手动脑(六)

日期:2018.11.8

星期四

博客期:022

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

  Part 1: 基本异常处理

 package teacher;

 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");
} }
}

AboutException.java

  运行结果如下:

Java的动手动脑(六)

  说明:因为当你的程序出现错误的时候,即第一个 k = i / j ;语句执行的时候,你的程序运行到这里就结束(中断)了,不可能继续运行try{}里的 k = i / j ;语句,所以会是这个结果!而当你将它的第一个 k = i / j; 用//或/**/注释掉之后 ,运行结果如下:

Java的动手动脑(六)

  在你注释掉了以后,它会继续执行try{}里的 k = i / j ;语句,也就会在try{}里抛出 ArithmeticException ,而后边刚好有catch 语句接到了,进而做了内部处理,由于 ArithmeticException 已经得到了 catch,后面的 catch (Exception e) 就没有接到,于是不执行,而后面的finally除了特殊情况,一般是要执行的,于是便是这样的结果!但如果你把第二个 k = i / j; 也用//或/**/注释掉之后,运行结果如下:

Java的动手动脑(六)

  理由很清晰,就是根据类型执行catch 语句和 finally 语句!

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

  Part 2: 多层的异常捕获(1+2)

 package teacher;

 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");
}
}
}

CatchWho.java

 package teacher;

 public class CatchWho2 {
public static void main(String[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException();
}
catch(ArithmeticException 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");
}
}
}

CatchWho2.java

  这两段代码的运行截图分别如下:

Java的动手动脑(六)

Java的动手动脑(六)

  说明:那我就把这两个案例综合在一起分析吧!首先对比这两个案例!我们可以看到区别,内部try{}里的所要catch的异常不同——ArrayIndexOutOfBoundsException和ArithmeticException,但究竟是什么原因导致了这结果的不同呢?我来说吧!这无外乎就是这一个catch的嵌套比较特殊!因为它两层的嵌套有重复的Exception监听处理,这就产生了另外一个问题——究竟是哪个或哪些部分catch到了异常呢?我们从运行结果中可以得知,catch是就近原则的,于是只要这个 Exception 被 catch 到了就不能再被非finally的catch语句catch到!所以就是这样的结果了!因为Exception不属于内部的catch的要求类型,因而没有被内部的catch语句catch到,而是外部的catch语句!所以第二个就会执行外部的catch操作!

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

  Part 3: 不同的finally语句的顺序

 package teacher;

 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"); } } }

EmbededFinally.java

  运行结果:

Java的动手动脑(六)

  说明:这个问题恰恰说的就是我Part 2所提到的问题——那个监听类型重复之后的顺序问题!就拿本程序来说,输出的顺序毫无疑问是1、2、3的顺序,而后面的finally的执行顺序却是3、2、1的顺序,当然这也是很容易理解的——毕竟还是按顺序来说,先执行try内部,当这个try的finally{}结束之后,外部的finally{}才能得以继续!所以finally的执行顺序是从内到外的!

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

  Part 4: 解析finally不执行的特殊情况

 package teacher;

 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(e.getMessage()); System.exit(0); } finally { System.out.println("in finally"); } } }

SystemExitAndFinally.java

  运行结果:

Java的动手动脑(六)

  说明:这个问题说明即使是finally也不能改变System.exit(0);能够直接退出运行状态的功能!这可能是finally唯一的不执行的情况!嗯,就目前来看吧,嗯!好像利用关机、任务管理器直接退出eclipse的,我就不说了!

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