• Java初学——面向对象异常


    一、异常

      1.什么是异常

        异常是指程序在运行是发生的不正常事件,会中断运行的程序称之为异常

      2.什么是异常处理

        Java编程语言使用异常处理机制为程序提供了错误处理的能力。一般处理顺序为:程序中预先设置好 对付异常的处理办法→程序运行→异常→对异常进行处理→处理完毕,程序继续运行

      3.Java中如何进行异常处理

        Java的异常处理是通过5个关键字来实现的:try、catch、 finally、throw、throws

      4.try catch

        

     1 public class text {
     2     public static void main(String[] args) {
     3         Scanner in = new Scanner(System.in);
     4         System.out.print("请输入被除数:");
     5         int num1 = in.nextInt();
     6         System.out.print("请输入除数:");
     7         int num2 = in.nextInt();
     8         try {//try中可能出现异常的代码
     9             System.out.println(num1+"/"+ num2 +"="+ num1/ num2);
    10             System.out.println("感谢使用本程序!");
    11         } catch (ArithmeticException e) {//在程序发生异常时候执行的代码 括号中变量是可能出现的异常类型
    12             System.out.println("除数不能为零");
    13         }
    14 
    15     }
    16 }

      Java中的异常类是存在继承关系的Exception父类可以代替任何子类,找出所有的异常,但是异常涉及面太广所以在设计代码时要精写,有时异常不仅仅是一个catch块,所以catch块可以多个存在多个。如以下代码

    public class text {
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            System.out.print("请输入被除数:");
            int num1 = in.nextInt();
            System.out.print("请输入除数:");
            int num2 = in.nextInt();
            try {// try中可能出现异常的代码
                System.out.println(num1 + "/" + num2 + "=" + num1 / num2);
                System.out.println("感谢使用本程序!");
            } catch (ArithmeticException e) {// 在程序发生异常时候执行的代码 括号中变量是可能出现的异常类型
                System.out.println("除数不能为零");
            } catch (NullPointerException e) {
                System.out.println("空指针异常");
            } catch (ClassCastException e) {
                System.out.println("类型转换异常");//如果没有满足上述异常执行顶级异常(未知异常)
            }catch (Exception e) {        //异常需要从小到大的顺序 将顶级异常放在最后
                System.out.println("顶级异常");
            }
    
        }
    }

     5.try catch finally语句

      使用finally语句无论是否出不出现异常都会执行。代码最先执行try语句 出现异常执行catch 在执行finally块,如果在代码中加入了system.exit(1)则直接退出虚拟机不会运行finally语句

        try catch 和try finally都能单独使用 但是不能省略try块且不能单独存在

    6.throw 和throws  

      throw:是抛出具体的某个对象,方法内部可能存在异常。请调用方处理 方法名末尾(在该方法中根本没有对该类异常的捕获)

      throws声明异常,通知调用方方法内部可能存在异常请调用方处理

      

    public class text {
        public static void main(String[] args) {
            try {
                b();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        public static void a () throws FileNotFoundException {//声明可能异常
        FileInputStream aFileInputStream=new FileInputStream("a.txt");
    
        }
        public static void b() throws FileNotFoundException {
            a();
        }
    }

    7.异常链

    使用异常链可以一层一层的去追溯错误的产生和传递,例如:我在mian方法中调用method1方法,在method1方法中调用method2方法,在method2方法中抛出一个空指针异常。

        

    public class Test {
    
        public static void main(String[] args) {
    
            Test t = new Test();
    
            try {
                t.method1();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void method1() throws Exception {
    
            try {
                method2();
            } catch (Exception e) {
                Exception exception = new Exception("method1 exception");
                exception.initCause(e);
                throw exception;
                //或者
                //throw new Exception("method1 exception", e);
            }
        }
    
        public void method2() {
            throw new NullPointerException("method2 exception");
        }
    }

    运行结果如下

    可以看到 method1 exception,然后Caused by method2 exception。之所以产生异常链,是因为

    Exception exception = new Exception("method1 exception");
    exception.initCause(e);
    throw exception;
    //或者
    //throw new Exception("method1 exception", e);

    如果不这样处理,打印如下

    java.lang.NullPointerException: method2 exception
        at normal.Test.method2(Test.java:31)
        at normal.Test.method1(Test.java:19)
        at normal.Test.main(Test.java:10)

                                   

  • 相关阅读:
    Swing中的并发使用SwingWorker线程模式
    使用批处理打二次开发包
    【转】批处理删除SVN文件
    EAS BOS ORMapping的研究
    EAS BOS ORMapping 元数据解析示例
    ORMapping学习
    EAS BOS数据查询默认会查分录的分析
    【Oracle】常用查询
    KDTable公式解析提示信息设置
    【转】Swing多线程编程
  • 原文地址:https://www.cnblogs.com/wuxuewei/p/10972100.html
Copyright © 2020-2023  润新知