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

黄舟
黄舟 原创
2017-02-04 10:09:42 678浏览

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

/*
 author by w3cschool.cc
 Main.java
 */public class Main {
   double method(int i) throws Exception{
      return i/0;
   }
   boolean method(boolean b) {
      return !b;
   }
   static double method(int x, double y) throws Exception  {
      return x + y ;
   }
   static double method(double x, double y) {
      return x + y - 3;
   }   
   public static void main(String[] args) {
      Main mn = new Main();
      try{
         System.out.println(method(10, 20.0));
         System.out.println(method(10.0, 20));
         System.out.println(method(10.0, 20.0));
         System.out.println(mn.method(10));
      }
      catch (Exception ex){
         System.out.println("exception occoure: "+ ex);
      }
   }
   }

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

30.0
27.0
27.0
exception occoure: java.lang.ArithmeticException: / by zero

以上就是Java 实例 - 重载方法异常处理的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。