zoukankan      html  css  js  c++  java
  • Java面试系列10()异常、断言

    代码可能在某个情况下执行会发生问题。
    需要预定义一个解决方案 进行处理 可能发生的异常.
    try{

    }catch(Exception e){

    }finally{

    }
    抛出异常:
    调用者

    public void show()throws Exception{}

    手动报错,
    public void show(){
    throw new Exception("msg");
    }

    Throwable
    Error Exception
    RuntimeException 运行时异常,不检查语法。
    CheckException 编译异常,检查语法异常

    RuntimeException类和子类都是运行异常
    NullPointer
    ArrayIndexOutOfBounds
    ClassCase
    ArithmeticException
    SystemException

    编译异常:
    ClassNotFound
    FileNotFound
    IOException
    SQLException
    EOFException

    异常栈:
    异常报错过程经过了多少方法

    断言

    if语句 的一个替代工具。if语句会跟着到机器码,断言则可以通过(-ea)设置。

    断言通常是在测试的时候使用。为了确保数据的正确性同时也要保证源码的不变性而使用断言。

    简单来说断崖是if 的一个替换,断言用于测试阶段,进不了机器码(项目上线一般不进)

    不过,真心不建议编码人员用断言,因为,你不知道测试人员知不知道你的意思。

    语法格式

    assert  bool表达式:”描述信息“;(下面一行是执行体)

    比如

    assert   str == ”asdf“ :”str不是asdf“;

    str.tostring();

    启动断言需要在启动程序时设置虚拟机参数 -ea

  • 相关阅读:
    poj 2584 T-Shirt Gumbo (二分匹配)
    hdu 1757 A Simple Math Problem (乘法矩阵)
    矩阵之矩阵乘法(转载)
    poj 2239 Selecting Courses (二分匹配)
    hdu 3661 Assignments (贪心)
    hdu 1348 Wall (凸包)
    poj 2060 Taxi Cab Scheme (二分匹配)
    hdu 2202 最大三角形 (凸包)
    hdu 1577 WisKey的眼神 (数学几何)
    poj 1719 Shooting Contest (二分匹配)
  • 原文地址:https://www.cnblogs.com/mcmx/p/11342120.html
Copyright © 2011-2022 走看看