提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
使用finally块
public class Main { public static void main(String[] args) { int x = 10, y = 0, z;/** from N o w J a v a . c o m**/ try { System.out.println("Before dividing x by y."); z = x / y; System.out.println("After dividing x by y."); } catch (ArithmeticException e) { System.out.println("Inside catch block - 1."); } finally { System.out.println("Inside finally block - 1."); } try { System.out.println("Before setting z to 2449."); z = 2449; System.out.println("After setting z to 2449."); }/**来 自 N o w J a v a . c o m**/ catch (Exception e) { System.out.println("Inside catch block - 2."); } finally { System.out.println("Inside finally block - 2."); } try { System.out.println("Inside try block - 3."); } finally { System.out.println("Inside finally block - 3."); } try { System.out.println("Before executing System.exit()."); System.exit(0); System.out.println(