zoukankan      html  css  js  c++  java
  • Exception异常 自定义异常

    public class Exception
        extends Throwable

    Exception 类及其子类是 Throwable 的一种形式,它指出了合理的应用程序想要捕获的条件。

    public class RuntimeException
        extends Exception

    RuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。 
    可能在执行方法期间抛出但未被捕获的 RuntimeException 的任何子类都无需在 throws 子句中进行声明。 

    1、异常的分类:

    ① 非运行时异常(Checked Exception) 
        Java中凡是继承自Exception但不是继承自RuntimeException的类都是非运行时异常 
    ② 运行时异常(Runtime Exception/Unchecked Exception) 
        a) RuntimeException类直接继承自Exception类,称为运行时异常。Java中所有的运行时异常都直接或间接的继承自RuntimeException. 
        Java中所有的异常类都直接或间接的继承自Exception。 

    2、异常的处理:

    对应非运行时异常,必须对其进行处理。处理方式有两种: 
    使用try…catch…finally语句块进行捕获 
    在产生异常的方法所在的方法声明throws Exception 
    对于运行时异常,可以不对其进行处理,也可以对其进行处理。一般情况下都不对其进行处理。 

    3、捕获异常:

    try {
        FileInputStream fs = new FileInputStream("D:/temp/a.txt");
    } catch (FileNotFoundException e) {
        System.out.println("catch...");
        e.printStackTrace();
    } finally{
        System.out.println("finally...");
    }

    4、抛出和捕获异常: 

     

     4.1、把异常交给JVM处理:

    public class ExceptionTest {
    
        public static void execute() throws Exception {
            System.out.println("execute...");
            throw new Exception();
        }
    
        public static void main(String[] args) throws Exception {
            execute();
        }
    }

    4.2、或者使用try catch语句块捕获异常:

    public class ExceptionTest {
    
        public static void execute() throws Exception {
            System.out.println("execute...");
            throw new Exception();
        }
    
        public static void main(String[] args) {
            try {
                execute();
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

    5、自定义异常:

    自定义异常通常是定义一个继承自Exception类的子类。一般情况下我们都会直接继承自Exception类,而不会继承某个运行时的异常类。 

    5.1、创建自定义异常:

    public class MyException extends Exception{
        public MyException(){
            super();
        }
        public MyException(String msg){
            super(msg);
        }
    }

    5.2、在类中使用异常:

    public class ExceptionTest {
    
        public static void execute(String a) throws MyException {
            System.out.println("execute...");
            if("true".equals(a)){
                throw new MyException("参数不能为 true");
            }
        }
    }

    5.3、捕获自定义异常:

    public static void main(String[] args) throws MyException {
    
        execute("true");
    }

    6、异常使用注意事项:

    当使用多个catch语句块来捕获异常时,需要将父类的catch语句块放到子类型的catch块之后,这样才能保证后续的catch可能被执行,否则子类型的catch将永远无法到达,Java编译器会报编译错误。 
    如果try语句块中存在return语句,那么首先会执行finally语句块中的代码,然后才返回。 
    如果try语句块中存在System.exit(0)语句,那么久不会执行finally语句块的代码了,因为System.exit(0)会终止当前运行的JVM。程序在JVM终止前结束执行。 

  • 相关阅读:
    poj3905 2sat!
    poj3648,2-sat求解
    poj2723 2sat判断解+二分
    hdu3622 2-sat问题,二分+判断有无解即可。
    poj2767,单向连通图判定,缩点+重新建图+新图DFS
    poj2186 求有向图G中所有点都能到达的点的数量
    poj2553 有向图缩点,强连通分量。
    poj 1236+hdu2767 有向图 缩点+看度数(tarjan)
    poj3694+hdu2460 求桥+缩点+LCA/tarjan
    dfs + 最小公倍数 Codeforces Round #383 (Div. 2)
  • 原文地址:https://www.cnblogs.com/wdpnodecodes/p/7406241.html
Copyright © 2011-2022 走看看