一.动手动脑 运行AboutException.java示例,了解Java中实现异常处理的基础知识。

1)源代码

import javax.swing.*;classAboutException {public static voidmain(String[] a) {double i=-1, j=0, k;k=i/j;try{k= i/j;    //Causes division-by-zero exception//throw new Exception("Hello.Exception!");
}catch( ArithmeticException e){System.out.println("被0除.  "+e.getMessage());}catch(Exception e){if (e instanceofArithmeticException)System.out.println("被0除");else{  System.out.println(e.getMessage());    }}finally{JOptionPane.showConfirmDialog(null,"OK  "+k);//JOptionPane.showInternalConfirmDialog(null, k);
}}}

2)结果截图

3)结果分析

当删去第一个k=i/j;结果正常运行,异常检测代码运用:

Try{//可能发生运行错误的代码;
}catch(异常类型   异常对象引用){//用于处理异常的代码
}finally{//用于“善后” 的代码(释放)}

二.使用Java异常处理机制

•把可能会发生错误的代码放进try语句块中。
•当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。catch语句块中的代码用于处理错误。
•当异常发生时,程序控制流程由try语句块跳转到catch语句块。
•不管是否有异常发生,finally语句块中的语句始终保证被执行。
•如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。

三.动手动脑  多层的异常捕获

1)源代码

public classCatchWho {public static voidmain(String[] args) {try{try{throw new ArrayIndexOutOfBoundsException(); //数组下标越界
}catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException" +  "/内层try-catch"); }throw new ArithmeticException(); //算术异常
}catch(ArithmeticException e) { //算数异常System.out.println("发生ArithmeticException"); }catch(ArrayIndexOutOfBoundsException e) { //数组下标越界System.out.println(  "ArrayIndexOutOfBoundsException" + "/外层try-catch"); } }
}

2)结果截图

3)结果分析
try的嵌套模式:处理内部try,抛出异常并接受处理。第一次处理完毕后,抛出第二次异常 处理第二次。

四.动手动脑  多层的异常捕获(2)

1)源代码

public classCatchWho2 {public static voidmain(String[] args) {try{try{throw newArrayIndexOutOfBoundsException(); }catch(ArithmeticException e) { System.out.println("ArrayIndexOutOfBoundsException" + "/内层try-catch"); }throw newArithmeticException(); }catch(ArithmeticException e) { System.out.println("发生ArithmeticException"); }catch(ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException   " + "/外层try-catch"); } }
}//抛出与接收一应一答

2)结果截图

五.动手动脑  当有多个嵌套的try…catch…finally时,要特别注意finally的执行时机。

1)源代码

public classEmbededFinally {public static voidmain(String args[]) {intresult;try{          System.out.println("in Level 1");try{                System.out.println("in Level 2");//result=100/0;//Level 2try{                   System.out.println("in Level 3");                     result=100/0;  //Level 3
}catch(Exception e) {System.out.println("Level 3:" +e.getClass().toString());                }finally{System.out.println("In Level 3 finally");                }//result=100/0;//Level 2
}catch(Exception e) {               System.out.println("Level 2:" +e.getClass().toString());           }finally{                System.out.println("In Level 2 finally");          }//result = 100 / 0;//level 1
}catch(Exception e) {           System.out.println("Level 1:" +e.getClass().toString());        }finally{System.out.println("In Level 1 finally");        }    }
}

2)结果截图

3)结果分析

当有多个嵌套的try…catch…finally时,异常在不同位置被接受,可能会导致异常下面的finally语句块执行顺序。

六.动手动脑  finally语句块一定会执行吗?

1)源程序

public classSystemExitAndFinally {public static voidmain(String[] args){try{System.out.println("in main");throw new Exception("Exception is thrown in main");//System.exit(0);
}catch(Exception e){            System.out.println(e.getMessage());  System.exit(0);}finally{            System.out.println("in finally");}    }
}

2)结果截图

3)结果分析

当图中System.exit(0);被执行时,结束程序,不再执行下面的finally.

七.动手动脑

编写一个程序,此程序在运行时要求用户输入一个 整数,代表某门课的考试成绩,程序接着给出“不及格”、“及格”、“中”、“良”、“优”的结论。
要求程序必须具备足够的健壮性,不管用户输入什 么样的内容,都不会崩溃。

import java.util.*;public classScore {static Scanner in=newScanner(System.in);public static voidmain(String[] args){System.out.println("输入成绩:");judge();}public static voidjudge(){int n=-1;while(n<0||n>100){try{n=in.nextInt();}catch(InputMismatchException e){System.out.println("格式错误!重新输入!");in.nextLine();}if(n>=0&&n<60)System.out.println("不合格!");else if(n>=60&&n<70)System.out.println("合格!");else if(n>=70&&n<80)System.out.println("中!");else if(n>=80&&n<90)System.out.println("良!");else if(n>=90&&n<=100)System.out.println("优!");else if(n>100)System.out.println("输入成绩不符");}}
}

2)结果截图

转载于:https://www.cnblogs.com/du1269038969/p/6102908.html

java 异常处理相关推荐

  1. Java异常处理12条军规

    摘要: 简单实用的建议. 原文:Java异常处理12条军规 公众号:Spring源码解析 Fundebug经授权转载,版权归原作者所有. 在Java语言中,异常从使用方式上可以分为两大类: Check ...

  2. Java 异常处理的 9 个最佳实践

    Java 异常处理的 9 个最佳实践 原文地址:https://dzone.com/articles/9-... 翻译出处:https://www.oschina.net/trans... 在 Jav ...

  3. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

  4. java异常处理之throw, throws,try和catch

    转自 http://blog.csdn.net/zhouyong80/article/details/1907799  程序运行过程中可能会出现异常情况,比如被0除.对负数计算平方根等,还有可能会出现 ...

  5. java提供两种处理异常的机制_浅析Java异常处理机制

    关于异常处理的文章已有相当的篇幅,本文简单总结了Java的异常处理机制,并结合代码分析了一些异常处理的最佳实践,对异常的性能开销进行了简单分析. 博客另一篇文章<[译]Java异常处理的最佳实践 ...

  6. java异常处理机制详解

    java异常处理机制详解 参考文章: (1)java异常处理机制详解 (2)https://www.cnblogs.com/vaejava/articles/6668809.html 备忘一下.

  7. java异常处理试题答案_Java 面试题和答案 - (下)

    第一篇讨论了面向对象编程和它的特点,关于Java和它的功能的常见问题,Java的集合类,垃圾收集器,本章主要讨论异常处理,Java小应用程序,Swing,JDBC,远程方法调用(RMI),Servle ...

  8. 简述java异常处理机制

    引言: Hello,我的好朋友们,又到我们相聚的时间了,今天我要和大家分享一些有关java异常处理的相关 知识,也是通过老师的讲解和相关材料的借鉴之后的一个比较系统的总结,真心希望写完这篇文章的我和看 ...

  9. Java异常处理及异常机制介绍

    Java异常处理及异常机制介绍 当出现程序无法控制的外部环境问题(用户提供的文件不存在,文件内容损坏,网络不可用...)时,JAVA就会用异常对象来描述. JAVA中用2种方法处理异常: 1.在发生异 ...

最新文章

  1. Struts2-整理笔记(三)结果处理跳转、获得servletAPI原生
  2. shiro 删除用户session_我的shiro之旅: 十二 shiro 踢出用户(同一用户只能一处登录)...
  3. ac 梦幻布丁 启发式合并
  4. 纪中C组模拟赛总结(2019.7.9)
  5. 互联网晚报 | 1月26日 星期三 | 春晚正式入驻视频号;小红书合并社区与电商业务;中国电信5G消息正式商用...
  6. A股开盘:深证区块链50指数跌0.52%,华闻集团跌停
  7. 为节省内存,动态添加view布局和控件
  8. 面向科研的推荐系统Benchmark诞生!
  9. mac共享文件夹 linux,一文搞定 Linux,Mac,Windows 的 NFS 网络文件共享服务部署
  10. Atitit spring cache key的生成 与ken生成规范 1. Good key name meth.params 1 1.1. Use epl 的mode but only clss
  11. 【379】pandas 说明
  12. FlashFXP,小编带你认识什么是FlashFXP软件
  13. 升级ios10默认ruby版本
  14. Java中print,printf,println的区别
  15. Linux WWW 服务器
  16. Python 卡尔曼滤波器实现
  17. 用MATLAB实现费诺编码
  18. Windows找不到文件无法卸载怎么解决?
  19. H3C网络搭建入门(H3C、Oracle、CRT三种软件的关联,以便于模拟实际)
  20. 如何在iPhone或iPad上设置动态壁纸

热门文章

  1. 单向链表的有关操作(链式存储结构)
  2. Hadoop集群系列(目录)
  3. PHP 面向对象:类的属性
  4. 软件工程师必备的24个软技能
  5. 建造者模式(工厂模式6)
  6. 30天敏捷结果(27):做些有重要意义的事
  7. 【干货分享】32本优秀的 JavaScript 免费电子书
  8. 主流框架中DOMContentLoaded事件的实现
  9. 根据客户中英文系统进入中英文页面
  10. android 虚拟设备的用法