日期:2018.11.8java
星期四eclipse
博客期:022ide
-----------------------------------------------------------------------------------------spa
Part 1: 基本异常处理3d


1 package teacher; 2 3 import javax.swing.*; 4 5 class AboutException { 6 public static void main(String[] a) 7 { 8 int i=1, j=0, k; 9 k=i/j; 10 11 12 try 13 { 14 15 k = i/j; // Causes division-by-zero exception 16 throw new Exception("Hello.Exception!"); 17 } 18 19 catch ( ArithmeticException e) 20 { 21 System.out.println("被0除. "+ e.getMessage()); 22 } 23 24 catch (Exception e) 25 { 26 if (e instanceof ArithmeticException) 27 System.out.println("被0除"); 28 else 29 { 30 System.out.println(e.getMessage()); 31 32 } 33 } 34 35 36 finally 37 { 38 JOptionPane.showConfirmDialog(null,"OK"); 39 } 40 41 } 42 }
运行结果以下:code
说明:由于当你的程序出现错误的时候,即第一个 k = i / j ;语句执行的时候,你的程序运行到这里就结束(中断)了,不可能继续运行try{}里的 k = i / j ;语句,因此会是这个结果!而当你将它的第一个 k = i / j; 用//或/**/注释掉以后 ,运行结果以下:blog
在你注释掉了之后,它会继续执行try{}里的 k = i / j ;语句,也就会在try{}里抛出 ArithmeticException ,然后边恰好有catch 语句接到了,进而作了内部处理,因为 ArithmeticException 已经获得了 catch,后面的 catch (Exception e) 就没有接到,因而不执行,然后面的finally除了特殊状况,通常是要执行的,因而即是这样的结果!但若是你把第二个 k = i / j; 也用//或/**/注释掉以后,运行结果以下:ip
理由很清晰,就是根据类型执行catch 语句和 finally 语句!get
-----------------------------------------------------------------------------------------博客
Part 2: 多层的异常捕获(1+2)


1 package teacher; 2 3 public class CatchWho { 4 public static void main(String[] args) { 5 try { 6 try { 7 throw new ArrayIndexOutOfBoundsException(); 8 } 9 catch(ArrayIndexOutOfBoundsException e) { 10 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 11 } 12 13 throw new ArithmeticException(); 14 } 15 catch(ArithmeticException e) { 16 System.out.println("发生ArithmeticException"); 17 } 18 catch(ArrayIndexOutOfBoundsException e) { 19 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 20 } 21 } 22 }


1 package teacher; 2 3 public class CatchWho2 { 4 public static void main(String[] args) { 5 try { 6 try { 7 throw new ArrayIndexOutOfBoundsException(); 8 } 9 catch(ArithmeticException e) { 10 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 11 } 12 throw new ArithmeticException(); 13 } 14 catch(ArithmeticException e) { 15 System.out.println("发生ArithmeticException"); 16 } 17 catch(ArrayIndexOutOfBoundsException e) { 18 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 19 } 20 } 21 }
这两段代码的运行截图分别以下:
说明:那我就把这两个案例综合在一块儿分析吧!首先对比这两个案例!咱们能够看到区别,内部try{}里的所要catch的异常不一样——ArrayIndexOutOfBoundsException和ArithmeticException,但到底是什么缘由致使了这结果的不一样呢?我来讲吧!这无外乎就是这一个catch的嵌套比较特殊!由于它两层的嵌套有重复的Exception监听处理,这就产生了另一个问题——到底是哪一个或哪些部分catch到了异常呢?咱们从运行结果中能够得知,catch是就近原则的,因而只要这个 Exception 被 catch 到了就不能再被非finally的catch语句catch到!因此就是这样的结果了!由于Exception不属于内部的catch的要求类型,于是没有被内部的catch语句catch到,而是外部的catch语句!因此第二个就会执行外部的catch操做!
-----------------------------------------------------------------------------------------
Part 3: 不一样的finally语句的顺序


1 package teacher; 2 3 public class EmbededFinally { 4 5 6 public static void main(String args[]) { 7 8 int result; 9 10 try { 11 12 System.out.println("in Level 1"); 13 14 15 try { 16 17 System.out.println("in Level 2"); 18 // result=100/0; //Level 2 19 20 try { 21 22 System.out.println("in Level 3"); 23 24 result=100/0; //Level 3 25 26 } 27 28 catch (Exception e) { 29 30 System.out.println("Level 3:" + e.getClass().toString()); 31 32 } 33 34 35 finally { 36 37 System.out.println("In Level 3 finally"); 38 39 } 40 41 42 // result=100/0; //Level 2 43 44 45 } 46 47 catch (Exception e) { 48 49 System.out.println("Level 2:" + e.getClass().toString()); 50 51 } 52 finally { 53 54 System.out.println("In Level 2 finally"); 55 56 } 57 58 // result = 100 / 0; //level 1 59 60 } 61 62 catch (Exception e) { 63 64 System.out.println("Level 1:" + e.getClass().toString()); 65 66 } 67 68 finally { 69 70 System.out.println("In Level 1 finally"); 71 72 } 73 74 } 75 76 }
运行结果:
说明:这个问题偏偏说的就是我Part 2所提到的问题——那个监听类型重复以后的顺序问题!就拿本程序来讲,输出的顺序毫无疑问是一、二、3的顺序,然后面的finally的执行顺序倒是三、二、1的顺序,固然这也是很容易理解的——毕竟仍是按顺序来讲,先执行try内部,当这个try的finally{}结束以后,外部的finally{}才能得以继续!因此finally的执行顺序是从内到外的!
-----------------------------------------------------------------------------------------
Part 4: 解析finally不执行的特殊状况


1 package teacher; 2 3 public class SystemExitAndFinally { 4 5 6 public static void main(String[] args) 7 { 8 9 try{ 10 11 12 System.out.println("in main"); 13 14 throw new Exception("Exception is thrown in main"); 15 16 //System.exit(0); 17 18 } 19 20 catch(Exception e) 21 22 { 23 24 System.out.println(e.getMessage()); 25 26 System.exit(0); 27 28 } 29 30 finally 31 32 { 33 34 System.out.println("in finally"); 35 36 } 37 38 } 39 40 41 }
运行结果:
说明:这个问题说明即便是finally也不能改变System.exit(0);可以直接退出运行状态的功能!这多是finally惟一的不执行的状况!嗯,就目前来看吧,嗯!好像利用关机、任务管理器直接退出eclipse的,我就不说了!
-----------------------------------------------------------------------------------------