FileReader reader = new FileReader(filename);
Scanner in = new Scanner(reader);
String input = in.next();
int value = Integer.parseInt(input);
System.out.println(value);
return true;
} finally {
// 一些关闭资源的操作
System.out.println("this is finally block!");
}
} catch (FileNotFoundException e) {
System.out.println("this is catch_for_filenot... block!");
return false;
}
}
}
3、自定义异常
毕竟系统自带的异常处理器并不能满足所有需求,因为对于我们开发人员来说,抛出的异常越细致,我们越容易找到问题,总不能所有的问题都抛出Exception吧?太笼统了。在实际的开发中,我们可以根据自己的需要,进行自定义异常处理器。
[java]
/**
* 自定义异常处理器,继承Exception或者RuntimeException,依情况而定.
* @author erqing
*
*/ www.2cto.com
public class NameNotSupportException extends RuntimeException {
private static final long serialVersionUID = 7295869280641332966L;
public NameNotSupportException() {
}
public NameNotSupportException(String message) {
super(message);
}
}
[java]
public class DefineTest {
public static void main(String[] args) {
String name = "egg";
if(!"erqing".equals(name)){
throw new NameNotSupportException("erqing");
}else{
System.out.println("name is OK!");
}
}
}
[java]
Exception in thread "main" NameNotSupportException: erqing
at DefineTest.main(DefineTest.java:7)