首先我必須說我讀了以下內容:http://docs.oracle.com/javase/tutorial/essential/exceptions/index.html!throwable不會編譯
我得到的,當我嘗試編譯錯誤是:
TestException.java:14: error: incompatible types
static void doRisky(String test) throws ScaryException
^
required: Throwable
found: ScaryException
TestException.java:19: error: incompatible types
throw new ScaryException();
^
required: Throwable
found: ScaryException
TestException.java:34: error: incompatible types
catch (ScaryException se)
^
required: Throwable
found: ScaryException
3 errors
我假設的錯誤信息給我一些暗示型的,但我就是不明白它是什麼。
import java.lang.*;
/** add this as to get it to compile */
class ScaryException extends Exception
{
/** how come I can't put code here? */
}
public class TestException {
static void doRisky(String test) throws ScaryException{
System.out.println ("start risky");
if ("yes".equals(test)) {
throw new ScaryException();
}
System.out.println("end risky");
}
public static void main(String [] args){
String test = "no";
try{
System.out.println("start try");
doRisky(test);
doRisky("yes");
System.out.println("end try");
}
catch (ScaryException se){
System.out.println("Got What " + se);
}
finally{
System. out. println("finally") ;
}
System.out.println("end of main");
}
}
您的'ScaryException'擴展了throwable的子類嗎?通常是一個'java.lang.Exception'? – PermGenError 2013-03-05 20:47:52