版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 新疆维吾尔自治区哈密市2027年高三一诊考试物理试卷(含答案解析)
- 2026年客服部一季度服务质量总结
- 工厂生产主管2026年上半年生产管控工作总结
- 2026年秋季初中开学第一课 感恩教育 感恩成长
- 2026年北师大版小学三年级数学上册课时《简单的几何图形》教案
- 康复推拿手法
- 主动脉硬化患者的护理
- 浅谈糖尿病饮食治疗
- 泌尿系统疾病的药物治疗
- DNA的复制、转录、翻译
- TCTBA 001-2019 非招标方式采购代理服务规范
- 人教PEP版(2024)三年级上册英语Unit 5《The colourful world》单元作业设计
- 梦境心理学:探索潜意识的奥秘
- 个人宾馆转让合同
- 2023年山东聊城职业技术学院招聘工作人员60人(第二批)笔试参考题库(共500题)答案详解版
- 行政复议法培训课件
- 住院医师规范化培训过程考核方案
- 华东师大版八年级数学上册知识点总结
- 履带吊安装拆除专项施工方案(汇集版)
- 焦化厂管理安全生产责任制
- JJG 130-2011工作用玻璃液体温度计
评论
0/150
提交评论