2012-12-18 87 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的命名约定,它会让你的代码更可读。 (类名应以大写字母开头)。

相关问题