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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 孟伟娟 中级黑马   /  2012-11-26 10:11  /  1257 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 孟伟娟 于 2012-11-26 10:12 编辑

编写能产生并能捕获ArrayIndexOutOfBoundsException异常的代码。
  1. class ArrayIndexOutOfBoundsException extends Exception{
  2.     public ArrayIndexOutOfBoundsException(String msg) {
  3.        super(msg);
  4.     }
  5. }
  6. class Demo {
  7.     public static Object array(Object[] obj,int index)throws ArrayIndexOutOfBoundsException{
  8.         if(index >= obj.length)
  9.             throw new ArrayIndexOutOfBoundsException("下标越界");
  10.        return obj[index];
  11.     }
  12. }
  13. public class Test3 {
  14.    public static void main(String[] args)throws ArrayIndexOutOfBoundsException {
  15.         String[] str = {"aa","bb","cc"};
  16.         try {
  17.              System.out.println(Demo.array(str,4));
  18.        } catch (ArrayIndexOutOfBoundsException e) {
  19.               e.printStackTrace();
  20.        }
  21.    }

  22. }
复制代码
各位大虾们可以提供更多的解决方案。谢谢!

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马