哪位大哥能幫我看下 為什么編譯器為一直報錯!
public class HelloWorld{
? ? ?public static void main(String []args){
? ? ?
? ? ?HelloWorld hello=new HelloWorld();
? ?
? ? ?try{
? ? ?hello.test2();
? ? ?}
? ? ?catch(Exception r){
? ? ?r.printStackTrace();
? ? ?
? ? ?}}
? ? ?
? ? ?
? ? ?
? ? ?public void test1()throws Exception{
? ? throw new DrunkException("開酒別喝車");
? ? }
? ? ? ? ?public void test2(){
? ? ? ? try {
? ? test1();
? ? } catch (DrunkException e) {
? ? // TODO 自動生成的 catch 塊
? ? RuntimeException newrun=new RuntimeException("司機一滴酒,親人兩行淚");
? ? newrun.initCause(e);
? ? throw newrun;
? ? }
? ? ? ? ?
? ? ? ? ?} ?
? ? ?
? ? ?
}
在test2里面 會一直報錯 ?test1();需要添加拋出說明或者用try、catch包圍
明明已經包圍了啊??!
2017-09-12
public void test1()throws Exception{ ??
這里改成public void test1()throws DrunkException{ ?就好了
但是我不知道為什么不能直接拋出基類。。。
2017-09-06
上面好像沒錯,你看看下面的代碼有沒有打錯?
public?class?DrunkException??extends?Exception{
????public?DrunkException(){
????????super();
????}
?????
????public?DrunkException(String?message){
????????super(message);//調用一下父類的有參構造方法
????}
? ?
2017-09-06
自定義的異常沒有寫吧