1. 正确关闭资源
2. finally
public class ExitHook { public static void main(String[] args) throws IOException { final FileOutputStream fos; fos = new FileOutputStream("a.bin"); System.out.println("打开物理资源"); Runtime.getRuntime().addShutdownHook(new Thread(){ public void run() { if (fos != null) { try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("关闭了物理资源"); } }); // 退出程序 System.exit(0); } }
关闭钩子负责在程序退出时回收系统的资源
3. catch块
4.继承得到的异常
原文:http://www.cnblogs.com/fang--/p/6272866.html