版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、第九章异常处理(Exceptions)9-1 简介执行Java程序时,若遇到错误程序代码,Java即以异常处理机制处理,并显示错误警语,提醒程序设计师有关事宜。有关重大错误,由系统内的错误警告例程执行,一般性错误,则由程序设计师自定义警告程序执行。系统内建的错误警告例程,可参考附录A,包括:java.lang的Error、java.lang的Exception、java.Util的Exception、java.io的Exception、java.awt的Exception、 的Exception。9-2 try/catch/finally代码块在Java类别程序块内(除了一般程序代码区域外)
2、可分割成数个特定意义的代码块。凡影响系统作业的程序代码必须要在“try代码块”执行。如果try代码块有引发编译错误的程序代码或引发错误的逻辑行为,则由“catch代码块”捕捉其错误,显示其错误信息及停止程序继续执行。“finally代码块”的程序代码为必须要执行的流程。图 9-1trycatchfinallycompiling1yes2yes3may work4no5no6no7no8may work范例129:创建网站与try/catch的应用01 import .*;02 import java.io.*; 03 public class Ex09_2_1_1 04 ServerSocke
3、t SS; 05 public Ex09_2_1_1() 06 SS = new ServerSocket(1234);07 System.out.println(Server created.);08 09 public static void main(String args)10 Ex09_2_1_1 ServerStart=new Ex09_2_1_1();11 12 范例130:创建网站与try/catch的应用01 import .*;02 import java.io.*; 03 public class Ex09_2_1_2 04 ServerSocket SS; 05 pub
4、lic Ex09_2_1_2() 06 try07 SS = new ServerSocket(1234);08 System.out.println(Server created.);09 10 catch(IOException e)11 System.out.println(e.getMessage();12 13 14 public static void main(String args)15 Ex09_2_1_2 ServerStart=new Ex09_2_1_2();16 17 范例131:不设try/catch代码块的应用01 class Ex09_2_2_1 02 publ
5、ic static void main (String args)03 04 int x; 05 x = 10 / 0;06 System.out.println(x= + x);07 08 范例132:try/catch代码块与异常事件数据库的应用01 class Ex09_2_2_2 02 public static void main (String args) 03 int x; 04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (ArithmeticException e) 09 System.out.pri
6、ntln(In ArithmeticException:+e.getMessage();10 11 12 范例133:catch代码块以Exception取代ArithmeticException的应用01 class Ex09_2_2_3 02 public static void main (String args) 03 int x;04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (Exception e) 09 System.out.println(In Exception :+e.getMessage();
7、10 11 12 范例134:finally代码块的应用 01 class Ex09_2_3_1 02 public static void main (String args) 03 int x; 04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 catch (Exception e) 09 System.out.println(In Exception :+e.getMessage();10 11 finally 12 System.out.println(In finally);13 14 15 范例135:依图9-1项3,
8、设置try代码块,不设置catch代码块,如果加置finally代码块也可能编译成功01 class Ex09_2_3_2 02 public static void main (String args) 03 int x;04 try 05 x = 10 / 0;06 System.out.println(x= + x);07 08 09 范例136:依图9-2-1项3,设置try代码块,不设置catch代码块,如果加置finally代码块也可能编译不成功 01 import .*;02 import java.io.*; 03 public class Ex09_2_3_3 04 Serv
9、erSocket SS;05 public Ex09_2_3_3() 06 try07 SS = new ServerSocket(1234);08 System.out.println(Server created.);09 10 public static void main(String args)11 Ex09_2_3_3 ServerStart=new Ex09_2_3_3();12 13 9-3 throws的用法Java使用try/catch代码块捕捉异常事件,尤其在处理重要事件时,为了系统安全,更是要求程序代码必须要置于try/catch代码块内执行,否则将发生编译错误。因此,
10、程序设计师在设计程序时,必须先考虑是否要使用try/catch代码块。为了减少程序设计师的困扰,可在方法程序(Methods) 或构造函数(Constructor) 声明“throws”,以取代try/catch的使用。范例137:方法程序或构造函数声明 “throws” 以取代try/catch的使用。 01 import .*;02 import java.io.*; 03 public class Ex09_3_1 04 ServerSocket SS; 05 public Ex09_3_1() throws IOException 06 SS = new ServerSocket(12
11、34);07 System.out.println(Server created.);08 09 public static void main(String args) throws IOException10 Ex09_3_1 ServerStart=new Ex09_3_1();11 12 范例138:方法程序或构造函数声明“throws”以取代try/catch的使用01 class Ex09_3_2 02 public static void main (String args) throws ArithmeticException 03 int x;04 x = 10 / 0;05
12、 System.out.println(x= + x);06 07 9-4 自定义异常处理对象自定义异常处理对象可分以下两种。以内建异常处理类别产生自定义新对象。自定义异常处理类别产生自定义新对象。范例139:自定义匿名新异常处理对象 01 class Ex09_4_1_1 02 public static void main (String args) 03 int x, y;04 try 05 x = 10;06 y = 0;07 if(y=0) throw new ArithmeticException(DIY Message);08 x = 10 / 0;09 System.out.p
13、rintln(x= + x);10 11 catch (ArithmeticException e)12 System.out.println(In ArithmeticException:+e.getMessage();13 14 15 范例140:自定义具体新异常处理对象01 class Ex09_4_1_2 02 public static void main (String args) 03 ArithmeticException f = new ArithmeticException(DIY Message);04 int x, y;05 try 06 x = 10;07 y = 0
14、;08 x = 10 / 0;09 System.out.println(x= + x);10 11 catch (ArithmeticException e)12 System.out.println(In Built Message: +e.getMessage();13 System.out.println(In DIY Message: +f.getMessage();14 15 16 范例141:自定义预定信息异常处理类与匿名新对象的应用01 class myException extends Exception 02 myException() 03 super(myExcepti
15、on Message);04 05 06 class Ex09_4_2_1 07 public static void main (String args) 08 int x, y;09 try 10 x = 10;11 y = 0;12 if(y=0) throw new myException();13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (myException e)17 System.out.println(In myException: +e.getMessage();18 19 20 范例142:自定义预定信息异
16、常处理类与具体新对象的应用01 class myException extends Exception 02 myException() 03 super(myException Message);04 05 06 class Ex09_4_2_2 07 public static void main (String args) 08 myException f = new myException();09 int x, y;10 try 11 x = 10;12 y = 0;13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (Ar
17、ithmeticException e)17 System.out.println(In Built Message: +e.getMessage();18 System.out.println(In myException Message: +f.getMessage();19 20 21 范例143:自定义信息异常处理类与匿名新对象的应用01 class myException extends Exception 02 myException(String msg) 03 super(msg);04 05 06 class Ex09_4_2_3 07 public static void
18、main (String args) 08 int x, y; 09 try 10 x = 10;11 y = 0;12 if(y=0) throw new myException(DIY Message);13 x = 10 / 0;14 System.out.println(x= + x);15 16 catch (myException e)17 System.out.println(In myException: +e.getMessage();18 19 20 范例144:自定义信息异常处理类与具体新对象的应用01 class myException extends Exception 02 myException(String msg) 03 super(msg);04 05 06 class Ex09_4_2_4 07 public static void main (String args) 08
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年交通安全水上安全培训
- 跟骨骨折并发症预防与护理
- 综合商务英语B1 U5
- 引流管护理的环境保护
- 2024-2025学年度冶金工业技能鉴定考前冲刺测试卷附完整答案详解(夺冠系列)
- 2024-2025学年临床执业医师通关考试题库及1套参考答案详解
- 2024-2025学年度辅警招聘考试能力提升B卷题库含答案详解【能力提升】
- 2024-2025学年度眉山职业技术学院单招数学能力检测试卷及答案详解(历年真题)
- 2024-2025学年山东外贸职业学院电视播音主持期末考试模考模拟试题【考点梳理】附答案详解
- 网络安全合规使用与管理承诺书范文6篇
- 有限空间及作业场所隐患图
- 2024年江苏中职职教高考统考语文试卷试题真题(精校打印)
- 长沙学法减分题库及答案
- DB31/T 1363-2022口腔综合治疗台水路卫生管理要求
- 啦啦操队形变化设计与编排
- 物联网工程专业本科主干课程教学大纲
- 中考道德与法治一轮专题复习课件专题四 生命的思考(含答案)
- 酒店厨房安全培训课件
- 《数学(下册)第8版》中职全套教学课件
- DL∕T 1441-2015 智能低压配电箱技术条件
- 酒店数字化运营概论 课件 项目四 酒店新媒体推广认知
评论
0/150
提交评论