Java異常的捕獲及處理(throws關鍵字)
·throws關鍵字主要用於方法宣告上,指的是當前方法之中異常後交給被呼叫處處理;
範例1:
class MyMath { public static int div(int x, int y) throws Exception { return x / y; } } public class Test { public static void main(String[] args) { try { System.out.println("計算結果:"+MyMath.div(10, 2)); } catch (Exception e) { e.printStackTrace(); } } }
===========分割線===========
範例2:
class MyMath {
public static int div(int x, int y) throws Exception {
return x / y;
}
}
public class Test {
public static void main(String[] args) throws Exception {
System.out.println("計算結果:" + MyMath.div(10, 2));
}
}