第八週動手動腦(2018.11.5-11.11)
阿新 • • 發佈:2018-11-08
動手動腦一:
請閱讀並執行AboutException.java示例
1 import javax.swing.*; 2 3 class AboutException { 4 public static void main(String[] a) 5 { 6 int i=1, j=0, k; 7 k=i/j; 8 9 10 try 11 { 12 13 k = i/j; // Causes division-by-zero exception 14 //View Codethrow new Exception("Hello.Exception!"); 15 } 16 17 catch ( ArithmeticException e) 18 { 19 System.out.println("被0除. "+ e.getMessage()); 20 } 21 22 catch (Exception e) 23 { 24 if (e instanceof ArithmeticException) 25 System.out.println("被0除");26 else 27 { 28 System.out.println(e.getMessage()); 29 30 } 31 } 32 33 34 finally 35 { 36 JOptionPane.showConfirmDialog(null,"OK"); 37 } 38 39 } 40 }