zoukankan      html  css  js  c++  java
  • java---异常

    纲要:

    1.异常的基本概念

    2.异常的分类

    3.异常的捕获与处理

    4.自定义异常

    5.方法的覆盖与异常

    内容:

    1.1 异常的基本概念

               Java中的异常(Exception)又称为例外,是一个在程序执行期间发生的事件,它中断正在执行的程序的正常指令流。为了能够及时有效地处理程序中的运行错误,必须使用异常类。

          注:java中采用类去模拟异常,异常是一类对象,对象是可以创建的。

                 例:NullPointerException e = 0x1234;

                        e是引用类型,保存的是指向堆内存中对象的地址,这个对象是NullPointerException类型,这个对象就是真实存在的一个事件,NullPointerException是一类                       事件。

    1.2 异常机制的作用

              java语言为我们提供了完善的异常处理机制

              作用是程序发生异常事件时,为我们输出详细的信息,程序员通过这个信息可以对异常进行处理,是程序更加健壮

    2.异常的层次结构图

    3.异常的捕获与处理

             1.声明抛出:

                 

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    public class Test {
    
        public static void main(String[] args) throws FileNotFoundException {
            m1();
        }
        
        public static void m1() throws FileNotFoundException {
            m2();
        }
        public static void m2() throws FileNotFoundException {
            m3();
        }
        
        public static void m3() throws FileNotFoundException{
            FileInputStream fis = new FileInputStream("Test.java");
            
        }
    }
    View Code

    用throws处理异常不是真正的处理异常,而是推卸责任,谁调用就抛给谁,上面的m1方法如果出现了异常,将会继续上抛给jvm,jvm遇到这个异常就会推出jvm。

                2.捕捉:

                  

    try{
        // 可能出现异常的代码
    }catch(异常类型1 变量){
    
        //处理异常的代码
    }catch(异常类型2 变量){
    
        //处理异常的代码
    }catch(异常类型3 变量){
    
        //处理异常的代码
    }........

    1.catch语句块可以有多个

    2.但从上到下catch,必须从小异常类型到大异常类型捕捉

    3.try...catch...语句块最多执行一个catch语句块,之行结束之后try...catch...就结束了。

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    
    public class Test {
    
        public static void main(String[] args) {
            try {
                FileInputStream fis = new FileInputStream("Test.java");
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            }
            System.out.println("Hello world!");
        }
    }

    输出: 

    Test.java (系统找不到指定的文件。)
    Hello world!

    1.使用try...catch...处理异常后下面的语句仍然执行

         finally

     finally语句块中的语句一定会执行,所以在程序中为保证某资源一定会释放,一般在finally语句块中释放释放资源

    public class Test {
    
        public static void main(String[] args) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("Test.java");
                
            } catch (FileNotFoundException e) {
                System.out.println(e.getMessage());
            }finally {
                //为保证资源一定会释放
                if(fis!=null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    虽然finally语句块的代码一定会执行,但也有例外

    public class Test {
    
        public static void main(String[] args) {
           try {
                System.exit(-1);
            }finally {
                System.out.println("Hello world");
            }
        }
    }

    Hello world不会输出

    4.自定义异常

             throw

             

    //自定义一个异常
    //以注册用户名为例,要求用户名不小于6位
    public class InvalidInputException extends Exception{
    
        public InvalidInputException() {
            
        }
        public InvalidInputException(String msg) {
            super(msg);//传入异常详细信息
        }
    }
    //顾客注册用户名代码
    public
    class CustomService { public void Register(String name) throws InvalidInputException{ //注册 if(name.length()<6) { throw new InvalidInputException("用户名不能小于6位"); } System.out.println("注册成功"); } }
    //测试类
    public class Test {
    
        public static void main(String[] args) {
            String name = "jack";//用户名
            
            CustomService c = new CustomService();
            try {
                c.Register(name);//调用注册方法
            } catch (InvalidInputException e) {
                
                System.out.println(e.getMessage());
            }
            
        }
        
    }

    输出:用户名不能小于6位

    5.方法的覆盖与异常

     重写的方法不能比被重写的方法抛出更宽泛的异常

  • 相关阅读:
    Poj 2391 二分答案+最大流+拆点
    POJ 1087 A Plug for UNIX 最大流
    POJ 1459 Power Network 最大流
    POJ 2112 Optimal Milking 二分答案+最大流
    POJ 1273 Drainage Ditches 最大流
    POJ 1149 PIGS 最大流
    POJ 2288 Islands and Bridges 哈密尔顿路 状态压缩DP
    The Cow Lexicon
    1523. K-inversions
    1350. Canteen
  • 原文地址:https://www.cnblogs.com/wwww2/p/11829387.html
Copyright © 2011-2022 走看看