《移动通信软件编程基础 - java语言》第5章
kurui
贡献于2012-01-12
3091
0
0
《移动通信软件编程基础 - java语言》第5章
下载需要
8
金币
[ 金币充值 ]
服务器/托管费、人工审核、技术维护等都需要很多费用,请您支持深度开源的发展
下载PPT
标签:
Java开发
Java
PPT 内容
1. 知识回顾类和对象的高级特征继承和Java实现多态和Java实现访问修饰符private、protected、public和默认修饰符方法修饰符static、final、abstract接口和多继承
2. 5异常 第章
3. 本章目标理解异常的概念运用Java实现的异常处理 掌握Java中异常的实现形式掌握Java中实现自定义异常
4. 异常 异常就是在程序的运行过程中所发生的异常事件,它中断指令的正常执行。public class ExceptionDemo { public static void main(String[] args) { int num1 = 0,num2 = 0; int result = 0; num1 = Integer.parseInt(args[0]); num2 = Integer.parseInt(args[1]); result = num1 / num2; System.out.println(num1 + " / " + num2 + " = " + result); } } 当num2为0值 程序运行出现异常
5. 演示 使用Java出现异常异常演示
6. 异常处理的方法 IF B IS ZERO GOTO ERROR C = A / B PRINT C GOTO EXIT ERROR: 异常处理块 DISPLAY EXIT: END
7. 超市 异常处理的方法 保安室 监控 保安 小偷
8. Java的异常类
9. Java的异常类 异常说明Exception异常层次结构的根类RuntimeException许多java.lang异常的基类ArithmeticException算术错误情形,如以零作除数IllegalArgumentException方法接收到非法参数ArrayIndexOutOfBoundsException数组大小小于或大于实际的数组大小NullPointerException尝试访问null对象成员ClassNotFoundException不能加载所需的类NumberFormatException数字转化格式异常,如字符串转换浮点IOExceptionI/O 异常的根类FileNotFoundException找不到文件EOFException文件结束InterruptedException线程中断
10. try-catch块 try { result = num1 / num2; System.out.println("计算结果为" + result); } catch(Exception e) { System.out.println(e.toString()); e.printStackTrace(); }
11. 演示 使用try-catch块实现异常处理try-catch块异常处理演示
12. try-catch-finally块 try { result = num1 / num2; System.out.println("计算结果" + result); } catch(Exception e) { System.out.println(e.toString()); e.printStackTrace(); } finally { System.out.println( "此块中释放资源" ); }
13. 演示 使用try-catch-finally块实现异常处理try-catch-finally块异常处理演示
14. 多重catch块 try块catch块catch块catch块异 常 类 由 子 类 到 父 类. . .try{ num1 = Integer.parseInt(args[0]); num2 = Integer.parseInt(args[1]); System.out.println("开始计算"); result = num1 / num2; System.out.println("计算结果" + result); } catch(ArrayIndexOutOfBoundsException e){ System.out.println("参数传入不足两个"); e.printStackTrace(); } catch(NumberFormatException e){ System.out.println("参数不是数字"); e.printStackTrace(); } catch(Exception e){ e.printStackTrace(); }
15. 演示 使用多重catch块实现异常处理多重catch块异常处理演示
16. 嵌套try-catch块 try{ num1 = Integer.parseInt(args[0]); try{ num2 = Integer.parseInt(args[1]); result = num1 / num2; System.out.println("计算结果" + result); } catch(ArithmeticException e) { System.out.println("除数不能为零"); } } catch(ArrayIndexOutOfBoundsException e){ System.out.println("参数传入不足两个"); } catch(NumberFormatException e){ System.out.println("参数不是数字"); } catch(Exception e){ e.printStackTrace(); } 嵌套 try-catch块 外部 try-catch块
17. 演示 使用嵌套catch块实现异常处理嵌套catch块异常处理演示
18. throw语句 public class ThrowDemo{ public static void main(String[] args){ … … num1 = Integer.parseInt(args[0]); num2 = Integer.parseInt(args[1]); System.out.println("开始计算"); try{ if (num2 == 0) throw new ArithmeticException(); result = num1 / num2; System.out.println("计算完毕:结果为" + result); } catch(Exception e){ e.printStackTrace(); } … … } }ArithmeticException 异常抛出异常捕获并处理异常 发现异常 通知 catch块
19. throws语句 class ThrowTest { void compute(String[] args) throws Exception { int num1 = 0,num2 = 0; int result = 0; num1 = Integer.parseInt(args[0]); num2 = Integer.parseInt(args[1]); System.out.println("开始计算"); if(num2 == 0) throw new Exception(); result = num1/num2; System.out.println("计算完毕:结果为" + result); System.out.println("计算过程结束"); } } 抛 出 异 常向上抛出异常
20. throws语句 public class ThrowDemo { public static void main(String[] args) { ThrowTest tt = new ThrowTest(); try { tt.compute(args); } catch(Exception e) { e.printStackTrace(); } System.out.println("运行结束"); } } 创建对象- compute方法 可以抛出异常当参数arg[1]为0 compute方法 抛出异常 发现异常 通知 catch块捕获并处理异常
21. 演示 使用throws实现从异常抛出异常 Throws语句演示
22. 自定义异常 class UserExceptionDemo extends ArithmeticException { UserExceptionDemo() { super("除数为零"); } } 第一步:创建异常类创建自定义异常类
23. 自定义异常 class UseUserException{ void compute(String[] args) throws UserExceptionDemo{ … … num2 = Integer.parseInt(args[1]); if(num2==0) throw new UserExceptionDemo(); result = num1 / num2; System.out.println("计算完毕:结果为" + result); } UseUserException(String[] args){ try{ compute(args); } catch(UserExceptionDemo e){ e.printStackTrace(); } } } 第二步:使用异常类抛出自定义异常参数args[1] = 0 抛出自定义异常
24. 自定义异常 public class UserException { public static void main(String args[]) { UseUserException uue = new UseUserException(args); System.out.println("程序运行结束"); } }第三步:测试异常类创建捕获自定义异常的对象
25. 演示 使用自定义异常 自定义异常演示
26. 总结异常异常是运行时发生的错误异常处理的方法异常类运行时异常非运行时异常Java管理异常处理的元素要监控的程序语句包含在 try 块内Java实现异常处理的结构catch 块中的代码用于捕获和处理异常在方法返回之前必须执行的代码应放置在 finally 块中要手动引发异常,使用关键字 throw任何被抛到方法外部的异常都必须用 throws 子句指定try-catch块try-catch-finally块多重catch块嵌套catch块Java实现自定义异常
27. 习题
28. 作业
PPT 图集
相关PPT
《移动通信软件编程基础—java语言》第1章
《移动通信软件编程基础 - java语言》第2章
《移动通信软件编程基础 - java语言》第3章
《移动通信软件编程基础 - java语言》第5章
《移动通信软件编程基础 - java语言》第4章
第2章 java语言基础
java_ppt第二章java语言基础
第 2 章 java语言基础
Java语言基础
Java 语言基础知识