⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java06_06.htm

📁 JAVA的课件
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>Java程序设计</title>
</head>

<body background="Bg.gif">

<p align="center"><font size="5"><b>§6.6 </b></font><b><font size="4">try-catch-finally语句的嵌套</font></b></p>    
    
<p align="left">  前面章节中,try-catch-finally都是单层的,未出现嵌套现象。实际上,和其余的块语句,诸如if,for,while一样,它也是可以嵌套使用的。也就是说,一个try-catch-finally可以嵌套在另一个try语句块的try、catch或finally部分。</p>    
<p align="left">&nbsp;&nbsp; 如果最内层的try语句中发生了异常,会先检测内层是否有匹配的catch语句,如果有,则交由此catch处理;如果没有匹配的,则逐层查找外层的catch语句,直到找到匹配的;如果所有的catch都不匹配,则交由系统处理。这个过程是通过异常堆栈实现的。我们先来看下面这个例子:</p>    
<p align="left">public static void CreateException() throws Exception{<br>    
&nbsp;&nbsp; double a=Math.random();<br>    
&nbsp;&nbsp; try{&nbsp;<br>   
&nbsp;&nbsp;&nbsp;&nbsp; if (a>0.5)<br>    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(a);<br>   
&nbsp;&nbsp;&nbsp;&nbsp; else<br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw&nbsp; new Exception();<br>    
&nbsp;&nbsp; }<br>   
&nbsp;&nbsp; catch (Exception el){<br>    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是在外层捕获的异常:"+el.toString());<br>  
&nbsp;&nbsp; }<br>   
&nbsp;&nbsp; finally{<br>  
&nbsp;&nbsp; System.out.println("这是外层的finally块");<br>  
&nbsp;&nbsp;&nbsp;&nbsp; try {&nbsp; //嵌套<br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = 10/0;<br>    
&nbsp;&nbsp;&nbsp;&nbsp; }<br>   
&nbsp;&nbsp;&nbsp;&nbsp; catch(ArithmeticException em){<br>    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;这是内层捕获的异常:"+em.toString());<br>   
&nbsp;&nbsp;&nbsp;&nbsp; }<br>  
&nbsp; &nbsp; finally{<br>  
&nbsp;&nbsp;&nbsp; &nbsp; System.out.println("这是内层的finally块");<br>  
&nbsp;&nbsp;&nbsp; }<br> 
&nbsp;&nbsp; }&nbsp;<br>   
&nbsp;}<br>   
&nbsp; 这个程序的流程可以很容易把握,再看下面这个程序:<br>  
public static void CreateException() throws Exception{<br>    
&nbsp;&nbsp; double a=Math.random();<br>    
&nbsp;&nbsp; try{&nbsp;<br>   
&nbsp;&nbsp;&nbsp;&nbsp; if (a>0.5)<br>    
&nbsp;&nbsp;&nbsp;&nbsp;{&nbsp; System.out.println(a);<br>  
&nbsp;&nbsp;&nbsp; try {&nbsp; //嵌套<br>   
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; a = 10/0;<br>    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>  
&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; catch(ArithmeticException em){<br>    
&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println(&quot;这是内层捕获的异常:"+em.toString());<br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>  
&nbsp;&nbsp;&nbsp; finally{<br>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是内层的finally块");<br>  
&nbsp;&nbsp;&nbsp;&nbsp; }<br>  
&nbsp;&nbsp; }<br>  
&nbsp;&nbsp;&nbsp;&nbsp; else<br>   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; throw&nbsp; new Exception();<br>    
&nbsp;&nbsp; }<br>   
&nbsp;&nbsp; catch (Exception el){<br>    
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是在外层捕获的异常:"+el.toString());<br>  
&nbsp;&nbsp; }<br>   
&nbsp;&nbsp; finally{<br>   
&nbsp;&nbsp; System.out.println("这是外层的finally块");<br>  
&nbsp;&nbsp; }&nbsp;<br>  
&nbsp;}  
</p>  
<p align="left">&nbsp;&nbsp; 像上面这个程序有两个嵌套的finally块,哪一个会先执行呢?如果内层和外层同时捕获同一个异常,那么会由哪个catch捕获呢?我们来看下面的例子:<br>  
public static void CreateException() throws Exception{<br>   
&nbsp;double a=Math.random();<br>   
&nbsp;try{&nbsp;<br>  
&nbsp; try {<br>   
&nbsp;&nbsp; a = 10/0;<br>   
&nbsp; }<br>  
&nbsp; catch(ArithmeticException em){<br>   
&nbsp;&nbsp;&nbsp; System.out.println("这是内层捕获的异常:"+em.toString());<br>  
&nbsp; }<br>  
&nbsp; finally{<br>  
&nbsp;&nbsp; System.out.println("这是内层的finally块");<br>  
&nbsp; }&nbsp;<br>  
&nbsp;}<br>  
&nbsp;catch (ArithmeticException el){<br>   
&nbsp;&nbsp; System.out.println("这是在外层捕获的异常:"+el.toString());<br>  
&nbsp;}<br>  
&nbsp;finally{<br>  
&nbsp; System.out.println("这是外层的finally块");<br>  
&nbsp;}&nbsp;<br>  
}<br>  
}  
</p>  
<p align="left">&nbsp;&nbsp;&nbsp;  
从这个例子可以看出,外层根本就无法捕获和内层同类型的异常,所以应该避免写出这样的程序。这种情况和我们在前一节的最后一个例子相同,道理也相同。 
</p> 
<p align="left">&nbsp;&nbsp; 
如果是在内层发生的异常,而内层并未捕获此异常,如果位置恰当,则可由外层来捕获;但外层发生的异常就不可能由内层来捕获。
</p>
<p align="left"><a href="index.htm">回目录</a>   <a href="java06_05.htm">上一课</a>  
<a href="java06_07.htm">下一课</a></p> 
 
</body> 
 
</html> 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -