【发布时间】:2011-02-07 14:27:58
【问题描述】:
我有 DirReader 和 Search 类。搜索使用 DirReader。我希望搜索知道 DirReader 何时抛出异常。那么如何才能有类抛出异常呢?
目前,我使用 initCorrect -dummy var。异常风格的方法可能更合适。
简化示例错误
$ javac ExceptionStatic.java
ExceptionStatic.java:4: '{' expected
public class ExceptionStatic throws Exception{
^
1 error
代码
import java.util.*;
import java.io.*;
// THIS PART NEEDS TO BE FIXED:
public class ExceptionStatic throws Exception{
private static boolean initCorrect = false;
public static String hello;
static{
try{
hello = "hallo";
//some other conditionals in real code
if( true) throw new Exception();
initCorrect=true;
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
if(initCorrect)
System.out.println(hello);
}
}
【问题讨论】:
标签: java exception class error-handling