JavaSE-异常

1、try catch finally 异常捕获

public class ExceptionTest {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        int c = 2;
        try {
            c = a / b;
            System.out.println("异常发生后");
        } catch (Exception e) {
            System.out.println("捕获异常...");
        } finally {
            System.out.println("finally ....");
        }

        System.out.println("异常结束:" + c);

    }
}
/*
  output:
   捕获异常...
   finally ....
   异常结束:2
 */

结论:发生异常后,finally中的代码是肯定会运行的,异常捕获之后的代码也会运行。

2、catch代码块中有return关键字,程序怎么处理

public class ExceptionTest2 {
    public static void main(String[] args) {
        int a = 1;
        int b = 0;
        int c = 2;
        try {
            c = a / b;
            System.out.println("异常发生后");
        } catch (Exception e) {
            System.out.println("捕获异常...");
            return;  //注意这个关键字
        } finally {
            System.out.println("finally ....");
        }

        System.out.println("异常结束:" + c);

    }
}
/*
  output:
    捕获异常...
    finally ....
 */

结论:在catch中有 return 关键字,finally中的代码也一定会执行,但是异常捕获之后的代码不会再运行了。

3、throw new Exception的使用

public class ThrowExceptionTest {
    public static void main(String[] args) {
        ThrowExceptionTest te = new ThrowExceptionTest();
        try {
            te.calculate(0);
        } catch (Exception e) {
            System.out.println("计算异常---->:" + e.getMessage());
            e.printStackTrace();
        }
    }

    public void calculate(int a) {
        try {
            int b = 2/a;  //这里会有异常
        } catch (Exception e) {
            System.out.println("进入catch");
            throw new BizException("计算错误");
        } finally {
            System.out.println("进入finally");
        }
    }
}
/*
  out.pring:
    进入catch
    进入finally
    计算异常---->:计算错误
 */

结论:在catch中抛出的异常会在调用它的上层方法中捕获到

public class ThrowExceptionTest2 {
    public static void main(String[] args) {
        ThrowExceptionTest2 te = new ThrowExceptionTest2();
        try {
            te.getFileUrl(null);
        } catch (Exception e) {
            System.out.println("获取文件异常---->:" + e.getMessage());
            e.printStackTrace();
        }
    }

    public String getFileUrl(String fileName) {
        if(StringUtils.isBlank(fileName)) {
            throw new BizException("文件名为空");
        }
        return fileName;
    }

}
/*
  out.pring:
   获取文件异常---->:文件名为空
 */

结论:普通语句中抛出的异常,会在调用它的上层方法中捕获到。

4、方法throws 异常(RuntimeException,Exception),调用它的上层方法是怎么处理的

public class ThrowRuntimeExceptionTest1 {
    public void f() throws RuntimeException{
        System.out.println("我抛出了RuntimeException");
    }

    public void g() throws Exception{
        System.out.println("我抛出了Exception");
    }

    public static void main(String[] args) {
        ThrowRuntimeExceptionTest1 test = new ThrowRuntimeExceptionTest1();
        test.f(); //这里不用捕获异常
        try {
            test.g(); //这里必须try catch,否则会编译出错
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

结论:方法 throws Exception,在调用它的上层方法必须要try catch,否则编译会出错。方法 throws RuntimeException,在调用它的上层方法不用强制try catch 不会有编译出错。

5、JDK7异常处理的新语法

public class Jdk7Exception {
    public static void main(String[] args) {
        try {
            System.out.println("代码片段.....");
        } catch (IllegalArgumentException | ArithmeticException e) {  //一个catch块中可以捕获多个异常
            e.printStackTrace();
        }
    }
}

参考:

   https://gitee.com/play-happy/base-project

猜你喜欢

转载自www.cnblogs.com/happyflyingpig/p/9620487.html