两道Java的题目,大家帮忙看看,答好加20分!

来源:百度知道 编辑:UC知道 时间:2024/06/05 03:42:22
第一题:大家看看这个程序为什么老是javac不过去?
//教材P253练习2
class UndefiniteExp extends Exception { }

class Apple{
public void f(Apple obj){
if(obj==null){
System.out.println("I'm JVM, I have throwed an exception waiting catcher to catch it!");
throw new UndefiniteExp();
}
else System.out.println("Excuate function f successfully!");
}
}

public class UndefiniteException{
public static void main(String[] args){
try{
Apple n1=new Apple(), n2;
n1.f(n2);
System.out.println("The program is aborted before me, so you won't see me is there is nothing wrong, orelse...");
}catch(UndefiniteExp exp){
System.out.println("Catcher, you have caught me!");
exp.printStackTrace(System.out);
}
System.out.println("You won't see me too, I have told you last time.");
}/*main*/
}/*class UndefiniteExcep

1
UndefiniteExp类没有抛出错误,n2没有初始化
class UndefiniteExp extends Exception { }

class Apple{
public void f(Apple obj) throws UndefiniteExp{
if(obj==null){
System.out.println("I'm JVM, I have throwed an exception waiting catcher to catch it!");
throw new UndefiniteExp();
}
else System.out.println("Excuate function f successfully!");
}
}

public class UndefiniteException{
public static void main(String[] args){
try{
Apple n1=new Apple(), n2 = null;
n1.f(n2);
System.out.println("The program is aborted before me, so you won't see me is there is nothing wrong, orelse...");
}catch(UndefiniteExp exp){
System.out.println("Catcher, you have caught me!");
exp.printStackTrace(System.out);
}
System.out.println("You won't see me too, I have told you last time.");
}/*main*/
}/*class Undefinit