JavaSE 第四章 异常处理、自定义异常类
发布日期:2021-04-30 21:11:31 浏览次数:94 分类:精选文章

本文共 3086 字,大约阅读时间需要 10 分钟。

4. ??

?????????????????????????????????????????????????????????????

4.1 ????

???????????????????????????????????????????????????????????????????????????????????????

???????????????????????????????????????????????????????????????????Java??????????

4.2 ??????

Java????????????????????????Throwable??Throwable?????????

  • Error???????????????????????????????????????????????????????

  • Exception???????????????????????????????Exception?????????

    • RuntimeException??????????????????????????????????????????????NullPointerException????????IndexOutOfBoundsException???????ArithmeticException??????????

    • Error?????????????????????

??????????????????????????????????????????????????????????

4.3 Exception???

???????????

  • ??????Runtime Exception??????????????????????

    • NullPointerException???? null ????????????
    • IndexOutOfBoundsException???????????????????
    • ArithmeticException??????????????

    ????????????????????????

  • ?????Non-Runtime Exception????????????????????

    • ParseException??????????????????
  • ?????????????????????????

    4.4 ????

    Java?????????????????????????

    • RuntimeException
    • ClassCastException????????????????????
    • ArrayIndexOutOfBoundsException???????????????????
    • NullPointerException???? null ????????????
    • ArithmeticException
    • IOException????????????????????????
    • FileNotFoundException????????????
    • EOFException???????????
    • ClassNotFoundException??????????
    • InterruptedException????????????
    • SQLException????????????

    ?????????????????????????????

    4.5 ????

    ????????????????Java????????????????

    4.5.1 ????

    ??????????

    try {
    // ?????????
    // ?????????????????catch?
    } catch (ExceptionType1 e1) {
    // ??ExceptionType1?????
    } catch (ExceptionType2 e2) {
    // ??ExceptionType2?????
    } finally {
    // ???????
    }

    ?????

    • try???????catch??
    • ??try???????????finally???????
    • finally???????????? cleanup ???

    ???

    public static void main(String[] args) {
    try {
    int[] arr = new int[3];
    System.out.println(arr[0]);
    System.out.println(arr[3]); // ?????ArrayIndexOutOfBoundsException
    System.out.println("over");
    } catch (ArrayIndexOutOfBoundsException e) {
    System.out.println("??????" + e.getMessage());
    } finally {
    System.out.println("?????");
    }
    }
    4.5.2 ??????

    ????????????????????????????????throws?????????

    public void method() throws IOException {
    // ????IOException???
    }

    ?????

    • ?????????????????????????????????????
    • ???????????????????????????????
    4.5.3 ?????????

    ?Java??Throwable????????????????

    • getMessage()??????????????
    • toString()?????????????????
    • printStackTrace()?????????????????????
    4.6 ?????

    ????????????????????????????????????Exception?RuntimeException????

    public class NoAgeException extends Exception {
    public NoAgeException(String message) {
    super(message);
    }
    }
    public class Person {
    private String name;
    private int age;
    public Person(String name, int age) throws NoAgeException {
    if (age < 0 || age > 200) {
    throw new NoAgeException("??????" + age);
    }
    this.name = name;
    this.age = age;
    }
    }

    ?????????????????????????????????????

    ???????????????????????????

    上一篇:如何用最小代价,迅速提高你项目中“shit”代码的可读性(妈妈再也不用担心我删库跑路啦)
    下一篇:这几本测试书籍强烈推荐!还有这必须关注的网站...

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2026年06月21日 10时32分03秒