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

📄 java06_04.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.4 处理异常</b></font></p>

<p align="left">  
如果需要对异常进行处理,首先要捕获异常,使用try-catch-finally语句可以实现这一任务。我们先回顾一下该语句的格式:</p>

<p align="left">&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可能产生错误需要被监视的语句<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(异常类型1&nbsp; 对象名){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 处理该异常的语句<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; catch(异常类型2&nbsp; 对象名){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 处理该异常的语句<br>
&nbsp;&nbsp;&nbsp; }<br>
&nbsp;&nbsp;&nbsp; finally{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
无论异常是否发生或是被捕获都会执行的语句<br>
&nbsp;&nbsp;&nbsp; }</p>
<p align="left">&nbsp;&nbsp; 这中间的catch语句可以有任意多个,但括号中的异常类型必须各不相同;finally块也是可选的,但catch和finally必须至少存在一个,不能光有try块。</p>
<p align="left">&nbsp;&nbsp; 当try中的某条语句引发了异常后,程序立即跳转到catch部分,查找和该异常类型相匹配的catch语句执行,而位于try中的该语句后面所有的语句都不再有机会执行。无论是否有匹配的catch语句,也无论是否发生了异常,程序最终都会转到finally中来执行。如果finally后面还有语句,那么它们是否被执行则要看是否有catch语句捕获了该异常而定。我们先来看下面的这个示例:</p>
<p align="left">class testException{<br> 
&nbsp;  public static void test(){<br> 
&nbsp;&nbsp;   int array[]=new int[10];<br> 
&nbsp;&nbsp;   try{<br> 
&nbsp;&nbsp;&nbsp;&nbsp;    array[10]=5;<br> 
&nbsp;&nbsp;   }&nbsp;<br>
&nbsp;&nbsp;   finally   {<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     System.out.println("这是finally块");<br> 
&nbsp;&nbsp;   }&nbsp;<br>
&nbsp;&nbsp;   System.out.println("函数正常结束");<br> 
&nbsp;  }<br>
<br>
&nbsp;  public static void main(String argv[]){<br> 
&nbsp;&nbsp;   test();<br> 
&nbsp;&nbsp;   System.out.println("程序正常结束");<br> 
&nbsp;  }&nbsp;<br>
}</p>
<p align="left">运行的结果是这样的:</p>
<p align="left">这是finally块java.lang.ArrayIndexOutOfBoundsException<br>
&nbsp;&nbsp;	at testException.test(testException.java:5)<br> 
&nbsp;&nbsp;	at testException.main(testException.java:20)<br> 
Exception in thread "main"</p> 
<p align="left">&nbsp;&nbsp; 程序在执行完finally中间的语句后立即转向了系统的异常处理块,打印出异常堆栈中的信息后程序就结束了。这种流程不受程序员的控制,显然不是我们想要的。</p>
<p align="left">&nbsp;&nbsp; 我们将上面的程序改动一下,加入catch捕获异常,变成下面这个样子:</p>
<p align="left">class testException{<br> 
&nbsp; public static void test(){<br> 
&nbsp;&nbsp; int array[]=new int[10];<br> 
&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp; array[10]=5;<br>
&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp; catch(ArrayIndexOutOfBoundsException e) {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("下标越界");<br>
&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp; finally {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是finally块");<br>
&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp; System.out.println("函数正常结束");<br>
&nbsp;}<br>
<br>
&nbsp;public static void main(String argv[]){<br> 
&nbsp;&nbsp; test();<br>
&nbsp;&nbsp; System.out.println("程序正常结束");<br>
&nbsp;}&nbsp;<br>
}</p>
<p align="left">程序运行的结果如下:</p>
<p align="left">下标越界<br>
这是finally块<br>
函数正常结束<br>
程序正常结束</p>
<p align="left">&nbsp;&nbsp; 
程序的流程虽然发生了变化,但并没有因为异常的发生而立即终止,仍然执行了后面的语句。我们把上面这个例子再改一下,看看由程序员捕获异常是如何保护程序流程的。</p>
<p align="left">class testException{<br> 
&nbsp; public static void test(){<br> 
&nbsp;&nbsp;&nbsp; int array[]=new int[3];<br> 
&nbsp;&nbsp;&nbsp; for (int i=3;i>=0; i--)<br> 
&nbsp;&nbsp;&nbsp; { try{<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[i]=i;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("array["+i+"]="+i);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; catch(ArrayIndexOutOfBoundsException e) {<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("下标越界");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; finally{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是finally块");<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; System.out.println("函数正常结束");<br>
&nbsp;}<br>
<br>
&nbsp; public static void main(String argv[]){<br> 
&nbsp;&nbsp;&nbsp; test();<br>
&nbsp;&nbsp;&nbsp; System.out.println("程序正常结束");<br>
&nbsp; }&nbsp;<br>
}</p>
<p align="left">运行结果如下:</p>
<p align="left">下标越界<br>
这是finally块<br>
array[2]=2<br>
这是finally块<br>
array[1]=1<br>
这是finally块<br>
array[0]=0<br>
这是finally块<br>
函数正常结束<br>
程序正常结束</p>
<p align="left">&nbsp;&nbsp;&nbsp; 
也就是发生异常后,只要该异常被程序员捕获处理过,并不影响后继的程序流程,在这里,后面的循环仍然可以继续执行(当然这个例子并没有太多的实际意义)。需要注意的是,上面这个例子如果改成这个样子就又有不同(Why?):</p>
<p align="left">class testException{<br> 
&nbsp;  public static void test(){<br> 
&nbsp;&nbsp;&nbsp;   int array[]=new int[3];<br> 
&nbsp;&nbsp;&nbsp; try{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i=3;i>=0; i--){<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; array[i]=i;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("array["+i+"]="+i);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;     }<br> 
&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp; catch(ArrayIndexOutOfBoundsException e) {<br> 
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("下标越界");<br>
&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; finally{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.out.println("这是finally块");<br>
&nbsp;&nbsp;&nbsp; }&nbsp;<br>
&nbsp;&nbsp;&nbsp; System.out.println("函数正常结束");<br>
&nbsp;  }<br>
</p>
<p align="left"><a href="index.htm">回目录</a>   <a href="java06_03.htm">上一课</a> 
<a href="java06_05.htm">下一课</a></p>

</body>

</html>

⌨️ 快捷键说明

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