现在的位置: 首页 > 综合 > 正文

Java异常 小例子

2012年03月07日 ⁄ 综合 ⁄ 共 728字 ⁄ 字号 评论关闭


j20exception继承自类Exception 



public class j20exception extends Exception


    public static void main(String args[])

   {

      String str1, str2;


      int i, j, result;


      str1 = new String(args[0]);

      str2 = new String(args[1]); 

 

      try

      {

         i = Integer.parseInt(str1);

         j = Integer.parseInt(str2);

         result = i / j;

         System.out.println("result = i / j = " + result);

      }

      catch (NumberFormatException e)

      {

         System.out.println("first exception:" + e.getMessage());

      }


      catch(ArithmeticException e)

      {

         System.out.println("second exception:" + e.getMessage());

      }


      finally

      {

         System.out.println("好像不过怎么样 finally都得执行");

      }

   }

}


结果如下图

Java异常 小例子 - 大灰狼 - 大灰狼 的博客

抱歉!评论已关闭.