• 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

  • 相关阅读:
    IDEA在debug时修改变量值
    CSS覆盖公共样式中的某个属性
    POI获取单元格的宽和高
    MySQL将一张表的某些列数据,复制到另外一张表,并且修改某些内容
    哈希是什么?为什么哈希存取比较快?
    工厂模式
    观察者模式
    instanceof判断的对象可以是接口
    JeeSite框架中httpSession.invalidate();无效
    HSQL可视化工具
  • 原文地址:https://www.cnblogs.com/happyflyingpig/p/9620487.html
Copyright © 2020-2023  润新知