2012-12-18 66 views
0

以下是「throw use;」顯示錯誤的代碼。爲什麼?如何爲用戶定義的異常使用throw?舉一些例子?對用戶定義的異常使用throw

class use extends Exception{ 
public String toString() { 
    return "too many exceptions"; 
} 
} 
class user{ 
public static void main(String s[]) { 
    int i=3; 
    try { 
     if(i>1) 
      throw use; 
    } 
    catch(use e) { 
     System.out.println(e.toString()); 
    } 
    finally{ 
     System.out.println("program executed successfully!"); 
    } 

} 
} 
+0

它顯示了什麼錯誤? –

回答

6

您需要的異常類的實例,把它扔到:

throw new use(); 

use a = new use(); 
throw a; 

今後請遵循Java的命名約定,它會讓你的代碼更可讀。 (類名應以大寫字母開頭)。

相關問題