Java 实例 - 重载方法异常处理


以下实例演示了重载方法的异常处理:

  1. /*
  2. author by shouce.ren
  3. Main.java
  4. */
  5.  
  6. public class Main {
  7. double method(int i) throws Exception{
  8. return i/0;
  9. }
  10. boolean method(boolean b) {
  11. return !b;
  12. }
  13. static double method(int x, double y) throws Exception {
  14. return x + y ;
  15. }
  16. static double method(double x, double y) {
  17. return x + y - 3;
  18. }
  19. public static void main(String[] args) {
  20. Main mn = new Main();
  21. try{
  22. System.out.println(method(10, 20.0));
  23. System.out.println(method(10.0, 20));
  24. System.out.println(method(10.0, 20.0));
  25. System.out.println(mn.method(10));
  26. }
  27. catch (Exception ex){
  28. System.out.println("exception occoure: "+ ex);
  29. }
  30. }
  31. }

以上代码运行输出结果为:

  1. 30.0
  2. 27.0
  3. 27.0
  4. exception occoure: java.lang.ArithmeticException: / by zero