下载此文档

java异常处理机制 java编程 java编程方案策划.ppt


文档分类:IT计算机 | 页数:约29页 举报非法文档有奖
1/29
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/29 下载此文档
文档列表 文档介绍
第六章:Java异常处理机制
题纲
什么是异常?
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
!
异常情况
异常
程序突然终止并将控制交给操作系统
在运行时发生的错误
Java异常
Java程序运行过程中所发生的异常事件可分为两类:
错误(Error):JVM系统内部错误、资源耗尽等严重情况
违例(Exception): 其它因编程错误或偶然的外在因素导致的一般性问题,例如:
对负数开平方根
空指针访问
试图读取不存在的文件
网络连接中断
Java异常举例
public class ExceptionTest01{
public static void main(String[] args) {
String friends[]={"lisa","bily","kessy"};
for(int i=0;i<5;i++) {
(friends[i]);
}
("\nthis is the end");
}
}
try 和 catch 块
try
catch
异常
执行 catch 后程序
继续正常运行
程序控制
引发
代码块
单元
try 和 catch 块
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) {
(“发生异常:" + ());
();
}
}
}
finally 块
try 块
finally 块
catch 块
无异常
异常
try、catch 和 finally 块的执行流程

java异常处理机制 java编程 java编程方案策划 来自淘豆网m.daumloan.com转载请标明出处.

相关文档 更多>>
非法内容举报中心
文档信息
  • 页数29
  • 收藏数0 收藏
  • 顶次数0
  • 上传人nnyoung
  • 文件大小620 KB
  • 时间2018-06-19