- 1、本文档共45页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
TP08 异常 ACCP 6.0 S2-3-使用Java实现面向对象编程 PPT教材.ppt
第八章;回顾与作业点评;预习检查;本章任务;本章目标;如何解决该问题呢?
;异常就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序;Java编程语言使用异常处理机制为程序提供了错误处理的能力;Java的异常处理是通过5个关键字来实现的:try、catch、 finally、throw、throws;;public class Test3 {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
System.out.print(请输入被除数:);
int num1 = in.nextInt();
System.out.print(请输入除数:);
int num2 = in.nextInt();
System.out.println(String.format(%d / %d = %d,
num1, num2, num1/ num2));
System.out.println(感谢使用本程序!);
} catch (Exception e) {
System.err.println(出现错误:被除数和除数必须是整数, + 除数不能为零。);
e.printStackTrace();
}
}
};;public class Test3 {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
System.out.print(请输入被除数:);
int num1 = in.nextInt();
System.out.print(请输入除数:);
int num2 = in.nextInt();
System.out.println(String.format(%d / %d = %d,
num1, num2, num1/ num2));
System.out.println(感谢使用本程序!);
} catch (Exception e) {
System.err.println(出现错误:被除数和除数必须是整数, + 除数不能为零。);
e.printStackTrace();
}
}
};printStackTrace的堆栈跟踪功能显示出程序运行到当前类的执行流程
;;try-catch块7-7;常见的异常类型 ;在try-catch块后加入finally块,可以确保无论是否发生异常,finally块中的代码总能被执行;public class Test4 {
public static void main(String[] args) {
try {
Scanner in = new Scanner(System.in);
System.out.print(请输入被除数:);
int num1 = in.nextInt();
System.out.print(请输入除数:);
int num2 = in.nextInt();
System.out.println(String.format(%d / %d = %d,
num1, num2, num1/ num2));
} catch (Exception e) {
System.err.println(“出现错误:被除数和除数必须是整数, +除数不能为零。);
System.out.println(e.getMessage());
} finally {
System.out.println(感谢使用本程序!);
}
}
};finally块中语句不执行的唯一情况
异常处理代码中执行System.exit(1)退出Java虚拟机 ;public void method(){
try {
// 代码段
// 产生异常(异常类型2)
} catch (异常类型1 ex) {
// 对异常进行处理的代码段
} catch (异常类型2 ex) {
// 对异常进行处理的代码段
} catch (异常类型3 ex) {
// 对异常进行处理的代码段
}
// 代码段
};多重catch块 2-2;练习——根据编号输出课程名称;共性问题集中讲解;声明异常;除了系统自动抛出异常外,有些问题需要程序员自行抛出异常 ;异常的
您可能关注的文档
- the first chapter saccharide chemistry 糖类化学 分子生物学教材.ppt
- the fourteenth chapter transfer and expression of genetic information 遗传信息的传递与 及表达 分子生物学课件.ppt
- the fourth chapter nucleic acid chemistry 核酸化学 分子生物学教材.ppt
- The Fun They Had 大学英语精读 教学教案.ppt
- The Future of the English 张汉熙高级英语第二册教材.ppt
- The Generalized Method of Moments知识讲稿.ppt
- The Globalization of China 报刊选读 教学文稿.ppt
- The Globalization of Chinese Brands 报刊选读 教学文稿.ppt
- The Government of The U.K. Britain and American culture 英美文化的.ppt
- The International Monetary System, 1870–1973 国际金融英文教材.ppt
文档评论(0)