zoukankan      html  css  js  c++  java
  • Day17 exception作业

    作业:

    1. final,finally 和 finalize的区别

    2. 定义三种新类型的异常。
    写一个类,在该类的三个方法中抛出三种不同的异常。
    然后在mian方法中调用这个类的不同方法,尝试用try catch捕获你写的异常。

    参考答案:

    1. final,finally 和 finalize的区别


    a. final 最终的意思,修饰类,变量(成员变量和局部变量),成员方法
    修饰类之后,该类不能被继承
    修饰变量之后,该变量变成自定义常量
    修饰方法之后,该方法部类被子类覆盖


    b. finally 修饰代码块
    finally代码块的执行特征是:
    1. 对于try-catch-finnally代码块而言,finally代码块中的代码,不管是否发生异常,
    finally代码块中的代码,最后都会执行
    2. 即使在finally代码块之前,有return语句,finally代码块,仍然会执行
    3. 特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))

    c. finalize() Object类中的一个方法
    该方法,在对象变成垃圾,并且被垃圾回收期调用之前,jvm会在该对象上调用 finalize()方法一次且一次

     

    2.

     1 public class Work2 {
     2 
     3     public static void main(String[] args) {
     4         ExceptionGenerator exceptionGenerator = new ExceptionGenerator();
     5 
     6         // 处理第一种自定义编译时异常
     7         try {
     8             exceptionGenerator.genFirstException();
     9         } catch (MyFirstException e) {
    10             e.printStackTrace();
    11         }
    12 
    13         // 处理第二种自定义运行时异常
    14         try {
    15             exceptionGenerator.genSecondException();
    16         } catch (Exception e) {
    17             e.printStackTrace();
    18         }
    19 
    20         // 处理第三种自定义编译时异常
    21         try {
    22             exceptionGenerator.genThirdException();
    23         } catch (MyThirdException e) {
    24             e.printStackTrace();
    25         }
    26     }
    27 
    28 }
    29 
    30 class ExceptionGenerator {
    31 
    32     // 抛出第一种编译时异常
    33     public void genFirstException() throws MyFirstException {
    34         throw new MyFirstException("第一种编译时类型的异常");
    35     }
    36 
    37     // 抛出第二种运行时异常
    38     public void genSecondException() {
    39         throw new MySecondException("第二种运行时类型的异常");
    40     }
    41 
    42     // 抛出第二种编译时异常
    43     public void genThirdException() throws MyThirdException {
    44         throw new MyThirdException("第三种编译时类型的异常");
    45     }
    46 }
    47 
    48 /*
    49  * 第一种自定义编译时异常
    50  */
    51 class MyFirstException extends Exception {
    52 
    53     public MyFirstException(String msg) {
    54         super(msg);
    55     }
    56 }
    57 
    58 /*
    59  * 第二种自定义运行时异常
    60  */
    61 class MySecondException extends RuntimeException {
    62     public MySecondException(String msg) {
    63         super(msg);
    64     }
    65 }
    66 
    67 /*
    68  * 第三种自定义编译时异常
    69  */
    70 class MyThirdException extends Exception {
    71     public MyThirdException(String msg) {
    72         super(msg);
    73     }
    74 }
  • 相关阅读:
    计算机网络基础 汇总
    指针与数组
    卡特兰数
    Leetcode Sort Colors
    Leetcode Group Shifted Strings
    Leetcode Summary Ranges
    Leetcode Count Primes
    Leetcode Reverse Words in a String II
    Leetcode Reverse Words in a String
    Leetcode Rotate Array
  • 原文地址:https://www.cnblogs.com/dust2017/p/12791511.html
Copyright © 2011-2022 走看看