异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况。许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象。例如:数组越界和被0除。

代码验证:

  1. package test;
  2.  
  3. import javax.swing.*;
  4.  
  5. class AboutException {
  6. public static void main(String[] a)
  7. {
  8. int i=1, j=0, k;
  9. try
  10. {
  11.  
  12. k = i/j; // Causes division-by-zero exception
  13. //throw new Exception("Hello.Exception!");
  14. }
  15.  
  16. catch ( ArithmeticException e)
  17. {
  18. System.out.println("被0除. "+ e.getMessage());
  19. }
  20.  
  21. catch (Exception e)
  22. {
  23. if (e instanceof ArithmeticException)
  24. System.out.println("被0除");
  25. else
  26. {
  27. System.out.println(e.getMessage());
  28.  
  29. }
  30. }
  31.  
  32. finally
  33. {
  34. JOptionPane.showConfirmDialog(null,"OK");
  35. }
  36.  
  37. }
  38. }

输出结果:

当java程序中出现多try catch的情况时,一定要注意程序执行的先后顺序。

多try catch的java异常处理代码一

  1. package test;
  2.  
  3. public class CatchWho {
  4. public static void main(String[] args) {
  5. try {
  6. try {
  7. throw new ArrayIndexOutOfBoundsException();
  8. }
  9. catch(ArrayIndexOutOfBoundsException e) {
  10. System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
  11. }
  12.  
  13. throw new ArithmeticException();
  14. }
  15. catch(ArithmeticException e) {
  16. System.out.println("发生ArithmeticException");
  17. }
  18. catch(ArrayIndexOutOfBoundsException e) {
  19. System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
  20. }
  21. }
  22. }

程序运行结果:

多try catch的java异常处理代码二

  1. package test;
  2.  
  3. public class CatchWho2 {
  4. public static void main(String[] args) {
  5. try {
  6. try {
  7. throw new ArrayIndexOutOfBoundsException();
  8. }
  9. catch(ArithmeticException e) {
  10. System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch");
  11. }
  12. throw new ArithmeticException();
  13. }
  14. catch(ArithmeticException e) {
  15. System.out.println("发生ArithmeticException");
  16. }
  17. catch(ArrayIndexOutOfBoundsException e) {
  18. System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch");
  19. }
  20. }
  21. }

程序运行结果:

当有多个try catch finally嵌套 时,要特别注意finally的执行时机。特别注意:当有多层嵌套的finally时,异常在不同的层次抛出,在不同的位置抛出,可能会导致不同的finally语句的执行顺序。再者,我们一般认为finally语句中的句子一定会被执行,这里的一定是相对而言的,并不是绝对的。例如以下程序代码的运行:

  1. package test;
  2.  
  3. public class SystemExitAndFinally {
  4.  
  5. public static void main(String[] args)
  6. {
  7.  
  8. try{
  9.  
  10. System.out.println("in main");
  11.  
  12. throw new Exception("Exception is thrown in main");
  13.  
  14. //System.exit(0);
  15.  
  16. }
  17.  
  18. catch(Exception e)
  19.  
  20. {
  21.  
  22. System.out.println(e.getMessage());
  23.  
  24. System.exit(0);
  25.  
  26. }
  27.  
  28. finally
  29.  
  30. {
  31.  
  32. System.out.println("in finally");
  33.  
  34. }
  35.  
  36. }
  37.  
  38. }

运行结果:

在这段java代码中finally语句块并没有执行。

通过过异常的学习,自己尝试了自定义了一个异常类来处理异常,源码如下:

  1. package classtest;
  2.  
  3. import java.util.Scanner;
  4.  
  5. class Myexception extends Exception {
  6. public Myexception(String message) {
  7. super(message);
  8. }
  9. }
  10.  
  11. public class Mytest {
  12.  
  13. public static void main(String[] args) throws Exception{
  14. // TODO Auto-generated method stub
  15. try{
  16. function();
  17. }
  18. catch (Exception e) {
  19. System.out.println(e.getMessage());
  20. System.out.println("main()函数运行时出现异常");
  21. }
  22. finally {
  23. System.out.println("main()函数运行完毕");
  24. }
  25. }
  26.  
  27. public static int judge(String str) {
  28. int c = 0;
  29. String regex = "[0-9]+";
  30. String regex1="[-][0-9]+";
  31. boolean d = str.matches(regex);
  32. boolean e = str.matches(regex1);
  33. if (d == false) {
  34. c = 1;
  35. if (e == true) {
  36. c = 0;
  37. }
  38. }
  39. return c;
  40. }
  41.  
  42. public static void function() {
  43. try {
  44. System.out.println("请输入一个正整数:");
  45. Scanner input = new Scanner(System.in);
  46. String a = input.next();
  47. int temp = judge(a);
  48. if (temp == 1) {
  49. throw new Myexception("不能输入非法字符");
  50. } else {
  51. int num = Integer.parseInt(a);
  52. if (num < 0)
  53. throw new Myexception("输入不能为负数");
  54. else if (num == 0) {
  55. throw new Myexception("正整数不包括0");
  56. }
  57. }
  58.  
  59. } catch (Myexception f) {
  60. System.out.println(f.getMessage());
  61. } catch (Exception e) {
  62. System.out.println(e.getMessage());
  63. System.out.println("function()函数运行时出现异常");
  64. }
  65. finally {
  66. System.out.println("function()函数已运行完毕,请指示下一步动作");
  67. }
  68.  
  69. }
  70. }

课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类的更多相关文章

  1. java——异常类、异常捕获、finally、异常抛出、自定义异常

    编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...

  2. Java异常类(Throwable)

    一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...

  3. java异常类的妙用

    异常类的妙用   以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过Throwable类的子类一同设置进去.今天在分析springSecurity3.0 ...

  4. Java异常类及处理

    异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...

  5. 面试准备(三) Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...

  6. Java 异常类层次结构

    在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...

  7. 每天一点点java---继承exception类来实现自己的异常类

    package prac_1; /** * <p>Title: 捕获异常和实现自己的异常类</p> * <p>Description: 通过继承Exception类 ...

  8. java语言课堂动手动脑

    1 运行 TestInherits.java 示例,观察输出,注意总结父类与子类之间构造方法的调用关系修改Parent构造方法的代码,显式调用GrandParent的另一个构造函数,注意这句调用代码是 ...

  9. java课堂动手动脑及课后实验总结

      动手动脑一:枚举   输出结果: false false true SMALL MEDIUM LARGE 分析和总结用法 枚举类型的使用是借助ENUM这样一个类,这个类是JAVA枚举类型的公共基本 ...

随机推荐

  1. 从零开始用golang创建一条简单的区块链

    区块链(Blockchain),是比特币的一个重要概念,它本质上是一个去中心化的数据库,同时作为比特币的底层技术,是一串使用密码学方法相关联产生的数据块,每一个数据块中包含了一批次比特币网络交易的信息 ...

  2. 多智能体系统(MAS)简介

    1.背景   自然界中大量个体聚集时往往能够形成协调.有序,甚至令人感到震撼的运动场景,比如天空中集体翱翔的庞大的鸟群.海洋中成群游动的鱼群,陆地上合作捕猎的狼群.这些群体现象所表现出的分布.协调.自 ...

  3. 《你不知道的JavaScript》笔记(一)

    用了一个星期把<你不知道的JavaScript>看完了,但是留下了很多疑惑,于是又带着这些疑惑回头看JavaScript的内容,略有所获. 第二遍阅读这本书,希望自己能够有更为深刻的理解. ...

  4. FILETIME类型到LARGE_INTEGER类型的转换

    核心编程第5版 245页到247页的讲到SetWaitableTimer函数的使用 其中提到 FILETIME类型到LARGE_INTEGER类型的转换问题,如下代码 //我们声明的局部变量 HAND ...

  5. Windows定时备份Mysql数据库

    1.新建批处理文件bat(随意命名:如auto_backup_mysql_data.bat) 2.在批处理文件里添加如下命令 %1 mshta vbscript:createobject(" ...

  6. js常用Matn函数的操练

    Math.PI console.log(Math.PI); 随机数以及向下取整 这是一个能实现从a-b之间随机打印一个整数 function rand_s(a, b) { var x = a + (b ...

  7. Golang 实现设计模式 —— 装饰模式

    概念 "用于代替继承的技术,无需通过继承增加子类就能扩展对象的新功能" "动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更为灵活" 何时 ...

  8. asp.net core learn

    .NET Core WebApi RESTful规范 RESTful API 最佳实践 理解RESTful架构 接口版本控制 Support multiple versions of ASP.NET ...

  9. 推荐一款可以丰富博文GIF免费录制工具——GifCam

    在网上写博文,看别人添加gif图片很好奇,于是尝试制作并传递了一次,确实挺有意思的,于是分享一下. gifcam是一个绿色的,可以录制GIF动图的小软件,十分适合用来录制电脑的各种操作. 在这里附上工 ...

  10. COGS 2089. 平凡的测试数据

    [题目描述] 树链剖分可以干什么? “可以支持在树中快速修改一个点信息,快速询问一条链信息” LCT可以干什么? “可以支持树链剖分支持的特性,并且支持快速链接两个棵树,或者断开某条边” 那我现在要出 ...