zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    第三天 python的初始编码,基本数据类型(int,str,bool),字符串的操作
    while循环,格式化输出,运算符,while...else...
    python的种类,变量,常量,基础数据类型,input,if条件语句
    JavaScript 之 web API
    传输层上的TCP和UDP
    应用层上的协议HTTP
    计算机网络通信
    grid布局
    使用classList和dataset实现tab切换
    JS之跨域
  • 原文地址:https://www.cnblogs.com/happyflyingpig/p/9620487.html
Copyright © 2011-2022 走看看