- 常规写法
public class HelloWord {
public static void main(String[] args) {
try{
throw new Exception("hello");
}catch (Exception e){
System.out.println("catch exception");
}finally {
System.out.println("finally");
}
}
}
- 同时捕获多个异常(jdk 1.7)
try{
throw new FileNotFoundException("hello");
}catch (ArithmeticException| IOException q){
System.out.println("catch exception");
}finally {
System.out.println("finally");
}
- TWR(try with source)(jdk 1.7),省去关闭流
File f = new File("a.text");
try(InputStream i = new FileInputStream(f)){
i.read();
}catch (Exception e){
System.out.println(e);
}