1 / 21
文档名称:

精品课件 JAVA编程过程中的异常.ppt

格式:ppt   页数:21
下载后只包含 1 个 PPT 格式的文档,没有任何的图纸或源代码,查看文件列表

如果您已付费下载过本站文档,您可以点这里二次下载

分享

预览

精品课件 JAVA编程过程中的异常.ppt

上传人:所以所以 2012/6/7 文件大小:0 KB

下载得到文件列表

精品课件 JAVA编程过程中的异常.ppt

文档介绍

文档介绍:第五章
异常
回顾
继承及其JAVA实现
多态及其JAVA实现
访问修饰符对类成员的访问限制
方法修饰符:static、final、abstract
2
目标
理解异常的概念
运用 try 块、catch 块和 finally 块处理异常
运用多重 catch 块处理异常
运用嵌套 try/catch 块处理异常
运用关键字 throw 和 throws 处理异常
运用JAVA编写和使用自定义异常
3
什么是异常?
public class ExceptionRaised {
public ExceptionRaised() {
}
public int calculate( int operand1, int operand2) {
int result = operand1 / operand2;
return result;
}
public static void main(String[] args) {
ExceptionRaised obj = new ExceptionRaised();
int result = (9, 0);
(result);
}
}
OS
!
异常情况
异常
程序突然终止并将控制交给操作系统
在运行时发生的错误
4
………
IF B IS ZERO GO TO ERROR
C = A / B
PRINT C
GO TO EXIT
ERROR:

处理异常的块“以零作除数,代码导致错误”

DISPLAY

EXIT:
END
处理异常 2-1
处理运行时错误的伪代码
5
手动引发异常
指定由方法引发的异常
try
finally
catch
throws
throw
处理异常 2-2
要监控的程序语句包含在此块中
以合理的方式
捕获和处理异常
释放资源等
6
Java异常类
文件结束
EOFException
找不到文件
FileNotFoundException
I/O 异常的根类
IOException
数字转化格式异常,比如字符串到 float 型数字的转换无效
NumberFormatException
不能加载所需的类
ClassNotFoundException
方法接收到非法参数
IllegalArgumentException
数组大小小于或大于实际的数组大小
ArrayIndexOutOfBoundException
尝试访问 null 对象成员
NullPointerException
许多 异常的基类
RuntimeException
异常层次结构的根类
Exception
算术错误情形,如以零作除数
ArithmeticException
线程中断
InterruptedException
说明
异常
7
try 和 catch 块 2-1
try
catch
异常
执行 catch 后程序
继续正常运行
程序控制
引发
代码块
单元
8
try 和 catch 块 2-2
演示:示例 1
try 和 catch 块的用法
class ExceptionRaised {
/** 构造方法. */
public ExceptionRaised() {
}
/**
* 这个方法运行时将会产生一个异常.
* ***@param operand1 除法中的分子
* ***@param operand2 除法中的分母
* ***@return int 返回除法的结果
*/
public int calculate(int operand1, int operand2) {
int result = operand1 / operand2;
return result;
}
}
public class ArithmeticException {
/** 构造方法. */
public ArithmeticException() {
}
public static void main(String[] args) {
ExceptionRaised obj = new ExceptionRaised();
try {
/* 定义变量 result 以存储结果. */
int result = (9, 0);
(result);
}catch (Exception e) {
(“发生异常