第五课《异常》
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
20
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
示例(3)
// 定义方法的时候已经定义了必须抛出了一个异常 private void getConn()throws SQLException{ System.out.println(); } public static void main(String[] args) { ThrowsDemo t = new ThrowsDemo(); try { //调用此方法的时候必须捕获或者throws继续向上一层抛, // 否则通不过编译 t.getConn(); } catch (SQLException e) { e.printStackTrace(); } }
15
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
finally语句
finally语句只用来控制从try—catch语句转移到另一 部分前的一些必要的善后工作,这些工作包含了关闭文 件或释放其他有关系统资源。finally语句执行的是一种 强制的无条件执行,即无论在程序中是否出现异常,也 不管出现的是哪一种异常,即使try代码块中包含有 break、continue、return或者throw语句,都必须执行 finally块中所包含的语句。 在出现和未出现异常的情况下都要执行的代码,可以放 到finally子句中
19
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
throws
如果一个方法可以导致一个异常但不处理它,就必 须在方法声明中包含一个throws子句。 一个throws子句列举了一个方法可能引发的所有 异常类型。(除Error类型以外) 一个方法可以引发的所有其他类型的异常必须在 throws子句中声明。如果不这样做,将会导致编译 错误
10
说 明 算术错误,如被 除 算术错误,如被0除 数组下标出界 数组元素赋值类型不兼容 非法强制转换类型 调用方法的参数非法 非法监控操作,如等待一个未锁定线 非法监控操作, 程 环境或应用状态不正确 请求操作与当前线程状态不兼容 某些类型索引越界 非法使用空引用 字符串到数字格式非法转换 数据操作异常 试图在字符串边界之外索引 遇到不支持的操作
示例(1)
public static void main(String[] args) { int i = 0; String greetings [] = {"Hello world!", "No, I mean it!","HELLO WORLD!!"}; while (i < 4) { System.out.println (greetings[i]); i++; } • }
• 异常类通常用来定义程序所遇到的轻微意外。可以写代码 来处理异常并继续程序执行,而不是让程序中断。 例如:发生下列情况时,会出现异常: 想打开的文件不存在 网络连接中断 受控操作数超出预定范围 正在装载的类文件丢失
3
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
17
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
异常的抛出
Java应用程序在运行时如果出现了一个可识别的错 误,就会产生一个与该错误相对应的异常类的对象,这 个对象包含了异常的类型和错误出现时程序所处的状态 信息,这个异常对象首先被交给Java虚拟机,由虚拟机 来寻找具体的异常处理者。在Java中把产生异常对象并 将其交给Java虚机的过程称为称为异常的抛出
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
异常处理
异常的处理主要包括捕捉异常、程序流程的跳转和异常 处理语句块的定义等。当一个异常被抛出时,应该有专 门的语句来捕获这个被抛出的异常对象,这个过程被称 为捕捉异常。当一个异常类的对象被捕捉或接收后,用 户程序就会发生流程的跳转,系统中止当前的流程而跳 转至专门的异常处理语句块,或直接跳出当前程序和 Java虚拟机回到操作系统
11
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
try和catch语句
在Java程序里,异常对象是依靠try/catch语句来捕捉和处理的。try/catch 异常处理语句块分为try语句块和catch语句块,其格式如下: try{ …… //try语句块,可能产生异常情况 }catch{ …… //catch语句块,对异常进行处理 } • 将能够抛出异常的代码放入try块中,然后创建相应的catch块的列表。 • 如果生成的异常与catch中提到的相匹配,catch条件的块语句就被执行。 • 在try块之后,可能有许多catch块,每一个都处理不同的异常。一定要将 特殊的异常写在前面
6
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
异常体系结构
7
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
分类说明
Error JVM系统内部错误,表示恢复不是不可能但 很困难的情况下的一种严重问题。比如说内存溢出。 不可能指望程序能处理这样的情况。 Exception 表示一种设计或实现问题,即如果程 序运行正常,从不会发生的情况(比如刚才的数组越 界)。
异常机制有什么作用
在程序中发生错误时,发现错误的方法能抛出一个 异常到其调用程序。然后,调用方法捕获该异常, 以一定的方法处理异常并继续程序执行。 这个方案给程序员一个写处理程序的选择,来处理 异常。 它为系统和用户之间提供了一种友好的交互方式。 增强程序的健壮性
4
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
16
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
示例(3)
public static void main(String[] args) { int i = 0; String greetings [] = {"Hello world!", "No, I mean it!","HELLO WORLD!!"}; try{ while (i < 4) { System.out.println (greetings[i]); i++; } }catch(Exception e){ System.out.println("捕获异常 捕获异常:"+e.toString()); 捕获异常 }finally{ System.out.println("finally 继续执行 继续执行"); } }
18
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
使用 throw
异常是通过关键字 throw 抛出,程序可以用throw语句引发 明确的异常。如: try { if(flag<0) { throw new NullPointerException(); } } • throw语句的操作数一定是Throwable类类型或Throwable 子类类型的一个对象
9
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
常见异常
异 常 ArithmeticException ArrayIndexOutOfBoundsException ArrayStoreException ClassCastException IllegalArgumentException IllegalMonitorStateException IllegalStateException IllegalThreadStateException IndexOutOfBoundsException NullPointerException NumberFormatException SQLException StringIndexOutOfBounds UnsupportedOperationException
5
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
运行结果
Hello world! No, I mean it! HELLO WORLD!! Exception in thread "main" ng.ArrayIndexOutOfBoundsException: 3 原因: 原因: 当i = 3是,按照循环中的规定,要输出的 greetings[3]不存在,超出了数组的边界,因而产生异 常。
设计catch块处理
在设计catch块处理不同的异常时,一般应注意如下 问题: (1) catch块中的语句应根据异常的不同而采取不同的处理方法,比较通 用的操作是打印异常和与该异常相关的信息,包括异常名称和产生异常 的方法名等。 (2) 由于异常对象与catch块的匹配是按照catch块的先后排列顺序进行的, 所以要处理多异常时应注意认真设计各catch块的排列顺序。 (3) 在用多catch语句时,记住异常子类必须在它们任何父类之前使用是 很重要的。这是因为运用父类的catch语句将捕获该类型及其所有子类 类型的异常。这样,如果子类在父类后面,子类将永远不会到达
第五课《异常》
目标
• 了解异常的定义 • 了解异常的分类 • 掌握 try、catch 和 finally 语句 的用法 • 掌握throw、throws子句的用法 • 掌握如何定义自己的异常
2
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
什么是异常?
8
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
异常类Exception
JAVA异常类都是系统类库中Exception类的子类。 Exception类是ng.Throwable类的一个子类,构造 函数为: public Exception(); public Exception(String s); 它从Throwable类那里继承了若干方法。常用的为: public String toString():返回异常类信息 public void printStackTrace():在当前标准输出 上输出异常信息
12
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
示例(2)
public static void main(String[] args) { int i = 0; String greetings [] = {"Hello world!", "No, I mean it!","HELLO WORLD!!"}; try{ try while (i < 4) { System.out.println (greetings[i]); i++; } } // 当try块发生异常的时候,异常会被catch所捕获 catch(Exception e){ catch System.out.println("捕获异常:"+e.toString()); } }
13
©2007 iSoftStone Holdings Ltd. All Rights Reserved.
多异常的捕获和处理
多异常处理是通过在一个try 块后面定义若干个catch块来实现的, 每个catch块用来接收和处理一种特 定的异常对象 。
14
©2007 iSoftStone Holdings Ltd. All Righபைடு நூலகம்s Reserved.