A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 许阳 黑马帝   /  2011-9-23 21:04  /  2026 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

Java中的异常:
       Java为异常提供了try-catch-finally语句来支持异常处理。try语句有关键字try和随后用于
   封装try块的大括号{}构成。try块包含可能产生异常的语句,并且在try块后必须至少紧跟一条
   catch块或一条finally块,每个catch块在圆括号中指定一个异常类参数,用于标识该类catch
   块能够处理的异常类型。在最后一个catch块后是一个可选的finally块,无论异常是否发生,都
   会执行该块所提供的代码。

   try{
          可能会出现异常情况的代码
   }catch(异常类型一 e1){
       处理出现异常一类型的代码
   }catch(异常类型二 e2){
       处理出现异常二类型的代码
   }

   try块中包含可能出现异常情况的代码,随后紧跟catch块,并且两个块中不允许出现其他的语句。

   异常分类:
      嵌套异常、
      public void test(String [] arg){
         try{
                      int num = Integer.parseInt(arg[1]);
                     
                   try{
                             int numValue = Integer.parseInt(arg[0]);
                             System.out.println(arg[0]+"的平方是"+numValue*numValue);
                   }catch(NumberFormatException ne){
                           System.out.println("不是一个数字!");
                   }
                }catch(ArrayIndexOutOfBoundsException ae){
                        System.out.println("请输入数字!");
         }
      }
      多重异常、

      class ExceptionCode{
         
         protected ExceptionCode(){
         
         }

         public void calculate(){
             try{
                  int num = 0;
                  int num1 = 42/num;
             }catch(ArithmeticException e){
                 System.out.println("父类异常catch子句");
             }catch(Exception e){
                 System.out.println("这个子类的父类是"+"exception类,且不能到达");
             }
         }
      }

      自定义异常、

   class FactorialCompute
   {
      protected FactorialCompute(){}

      public static long factorial(final int x)
        {
         long fact=1;
                 if(x<0)
                    {
                      throw new lllgalArgumentException("x必须为非负数");
                    }
                 for(int i=1;i<=x;i++)
                         {
                       fact*=i;
                    }

        }
        return(fact);
  }

     public static void main(String [] args)
     {
        int empld,deptld;
        String empName;
  
        try
          {
              empld=Integer.parseInt(args[0]);
              empName=args[1];
              deptld=Integer.parseInt(args[2]);

              if(deptld>5)
                {
                     throw new DepartmentException();
                }
              else
                {
                    System.out.println("雇员编号是:"+empld);
                    System.out.println("雇员姓名是:"+empName);
                    System.out.println("部门编号是:"+deptld);                    
                }
         }catch(DepartmentException de){System.out.println("数据无效");}
  
          catch(NumberFormatException ne){System.out.println("数据错误");}

          catch(ArrayIndexOutOfBoundsException ae){System.out.println("数据不完整");}
     }

     java异常分类还有哪些遗漏?请各位马友帮忙!

评分

参与人数 1技术分 +1 收起 理由
wangfayin + 1 等等答案……

查看全部评分

1 个回复

倒序浏览
黑马网友  发表于 2011-9-24 01:21:20
沙发
不建议分太细
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马