在千峰“逆战班”学习的第27天
今天的学习内容是对异常中的自定义异常、带有异常声明的方法覆盖以及对异常的一些知识扩充
中国加油!武汉加油!千峰加油!我自己加油!!!
作业:
5.
public class Test {
public static void main(String[] args){
int n = 3;
try{
if(n % 2 == 0){
m1();
}else{
m2();
}
}catch(MyException2 e){
System.err.println(e.getMessage());
}catch(MyException1 e){
System.err.println(e.getMessage());
}
}
public static void m1(){
throw new MyException2("haha");
}
public static void m2() throws MyException1{
throw new MyException1("hehe");
}
}
class MyException1 extends Exception{
public MyException1(){}
public MyException1(String message){
super(message);
}
}
class MyException2 extends RuntimeException{
public MyException2(){}
public MyException2(String message){
super(message);
}
}
public class Test {
public static void main(String[] args){
int n = 2;
try{
m(n);
}catch(MyException1 ex1){
ex1.printStackTrace();
}catch(MyException2 ex2){
System.err.println(ex2.getMessage());
throw ex2;
}
}
public static void m(int n) throws MyException1{
if(n == 1){
throw new MyException1("n == 1");
}else{
throw new MyException2("n == 2");
}
}
}
class MyException1 extends Exception{
public MyException1(){}
public MyException1(String message){
super(message);
}
}
class MyException2 extends RuntimeException{
public MyException2(){}
public MyException2(String message){
super(message);
}
}
8.AB D
12.不能通过
MySub2中的m()方法抛出的异常不能比父类中的m()方法抛出的异常更宽泛,应该把FileNotFoundException更改为EOFException或者其子类