千锋逆战班:孙华建
在千锋学习第27天
“未来的你会感谢今天奋斗的自己”
今天我学习了java课程,自定义异常
#中国加油!武汉加油!千锋加油!也为自己加油!!!#…
5,
public class TestException {
public static void main(String[] args) {
}
}
class MyException1 extends Exception{
public MyException1(){}
public MyException1(String message){}
}
class MyException2 extends RuntimeException{
public MyException2(){}
public MyException2(String message){}
}
6,
public class TestException {
public static void main(String[] args) {
int n = 1;
//读入n
try{
m(n);
}catch(MyException1 ex1){
//输出ex1详细的方法调用栈信息
ex1.printStackTrace();
}catch(MyException2 ex2){
//输出ex2的详细信息
System.out.println(ex2.getMessage());
//并把ex2重新抛出
throw ex2;
}
}
public static void m(int n)throws MyException1{
if(n == 1){
//抛出MyException1
//并设定其详细信息为"n==1"
throw new MyException1("n == 1");
}else{
//抛出MyException2
//并设定其详细信息为"n==2"
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,
//1–AB; //2–D
10,
100;100
12
不能通过,修改:
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
class MySuper {
public void m() throws IOException{}
}
class MySub extends MySuper{
public void m()throws EOFException{}
}
class MySub2 extends MySuper{
public void m()throws FileNotFoundException{}
}