异常处理
1、埔获异常,记录异常并响应合话的信息给用户
public static void main ( String [ ] args) {
try {
test1 ( ) ;
} catch ( FileNotFoundException e) {
system. out. println ( "您要找的文件不存在!! " ) ;
e. printstackTrace ( ) ;
} catch ( ParseException e) {
system. out. println ( "您要解析的时间有问题了! " ) ;
e. printStackTrace ( ) ;
}
}
public static void test1 ( ) throws FileNotFoundException ,ParseException {
SimpleDateFormat sdf = new SimpleDateFormat ( pattern: "yyyy-MM-dd HH: mm : ss" ) ;
Date d = sdf. parse ( source: "2028-11-11 10:24" ) ;
system. out. println ( d) ;
test2 ( ) ;
}
public static void test2 ( ) throws FileNotFoundException {
Inputstream is = new FileInputStream ( name: "D:/meinv.png" ) ;
}
另外可以全部都用Exception来抛出异常,这样代码就会更简单一些
2.捕获异常,尝试重新修复
当输入的不是数字的话,程序直接崩了,会把异常抛出来。
此时要抓住异常,然后修复