import java.io.*;
class MyException1
{
static String str="";
public static void main(String args[])
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your food");
try{
str=br.readLine();
}catch(IOException e){
System.out.println("Exception has been occurred"+e);
}
try{
checkFood();
}catch(BadException be){
System.out.println("Exception"+be);
}
}
private static void checkFood() throws BadException
{
if(str.equals("Rotten")|| str.equals("")){
System.out.println("Bad food");
//throw new BadException();
throw new BadException("Not Eatable");
}else{
System.out.println("Good food !! enjoy");
}
}
}
class BadException extends Exception
{
String food;
BadException()
{
super();
food="invalid";
System.out.println(food);
}
BadException(String s)
{
super(s);
food=s;
}
public String getError()
{
return food;
}
}
在程序中,這個public String getError()
怎麼返回food
變量?我沒有把它叫到任何地方?當從構造函數中調用關鍵字「super」時如何工作?
如果我刪除了行super(s);
,則「不可打印」不會打印。但是如果我留下這一行,那麼它會打印出來。這個程序如何工作?
「懷疑」意味着你相信某事是*不真實*,而不是你*困惑*關於某事。告訴你的朋友。 – user359996 2010-10-28 07:17:15
@ user359996:我只是喜歡這部分,「告訴你的朋友」。 ;) – 2010-10-28 07:45:00