




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、例1:public class Try1 public static void main (String args) int i=0; int a = 5,6,7,8; for(i=0;i<5;i+) System.out.println(" a"+i+"="+ai); System.out.print("3/0="+(3/0); 例2:public class Try2 public static void main (String args) int i=0; int a = 5,6,7,8; for(i=0;i<5;
2、i+) try System.out.print("a"+i+"/"+i+"="+(ai/i); catch(ArrayIndexOutOfBoundsException e) System.out.print("捕获数组下标越界异常!"); catch(ArithmeticException e) System.out.print("捕获算术异常!"); catch(Exception e) System.out.print("捕获"+e.getMessage()+&quo
3、t;异常!"); /显示异常信息 finally System.out.println(" finally i="+i); System.out.println("继续!"); 练习:分析程序运行结果。运行验证public class catchlx static void setN(int n) System.out.println("n的值是:"+n) ; try if (n=0) System.out.println("没有捕获异常!") ; return ; else if (n=1) int i
4、=0;int j=4/i; else if (n=2) int a=new int4;a4=10; catch(ArithmeticException e) System.out.println("捕获的异常信息是:"+e) ; catch(ArrayIndexOutOfBoundsException e) System.out.println("捕获的异常信息是:"+e) ; catch(Exception e) System.out.println("本句没有执行!") ; finally System.out.println(&
5、quot;finally 块!") ; public static void main(String args) setN(0); setN(1); setN(2); 3.调用方法处理异常(即发生异常的方法中不对异常处理,而是由调用者来处理异常) public calss ExceptionSamplepublic static void main(String args) int z; try z=Division(10,0); catch(Exception e) z=10; System.out.println(z);static int Division(int x, int
6、 y) throws ArithmeticException return x/y; 做如下修改,结果会怎样?public calss ExceptionSamplepublic static void main(String args) throws ArithmeticException int z; z=Division(10,0); System.out.println(z);static int Division(int x, int y) throws ArithmeticException return x/y; 例:用throw抛出异常。class throwSample pu
7、blic static void main(String args)try if (args.length!=1) throw new Exception(“参数过少或过多”); else System.out.println(agrs0);catch(Exception e) System.out.println(“*参数个数不对!”); System.out.println(e.getMessage(); 例:自定义异常public calss MyException extends Throwable public MyException() Super(); public MyExce
8、ption(String msg) Super(msg); public MyException(String msg, Thrwoable cause) Super(msg,cause); public MyException(Thrwoable cause) Super(cause); public class Exceptonsabple public static void main(String args)try throw new MyException(“自定义异常!”); Catch (MyException me) System.out.println(me.getMessa
9、ge(); 自定义异常练习:例如:class MyException extends Exception /自定义异常 private int detail; MyException(int a) detail=a; public String toString() return "myexceptoin = "+detail; public class yichang static void compute(int a) throws MyException /有异常抛出的方法 System.out.println("调用 compute("+a+")") ; if (a>10) throw new MyException(a); System.out.println(" 正常退出 ") ; public static void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年初级的电子电路设计模拟题集及解析
- 2025年乡镇农技站招聘农业物联网维护员考试重点与难点解析
- 抹灰安全教育培训记录课件
- 2025年低熔点金属胶合作协议书
- 2025年大坝建设项目发展计划
- 辽宁省葫芦岛市连山区2025-2026学年上学期七年级开学摸底考试数学试卷(含部分简单答案)
- 2025年微机励磁屏项目合作计划书
- 职教云诊断学题库及答案
- 考试英语搞笑题及答案
- 技能培训知识课件
- 广州市南沙区卫生健康局招聘下属事业单位工作人员考试真题2024
- 安装调试服务合同(2025版)
- 2025年锅炉专业培训试题及答案
- 泌尿外科规培生入科宣教
- 支气管镜入门图谱
- 中职信息技术第一册课件
- 高三职业生涯规划课件
- 上汽大众品牌培训课件
- 《礼仪规范教程》中职生礼仪教学全套教学课件
- 电力调度程控交换机安装施工方案
- 2025年广东省中考历史试题卷(含答案详解)
评论
0/150
提交评论