本帖最后由 孟伟娟 于 2012-11-26 10:12 编辑
编写能产生并能捕获ArrayIndexOutOfBoundsException异常的代码。- class ArrayIndexOutOfBoundsException extends Exception{
- public ArrayIndexOutOfBoundsException(String msg) {
- super(msg);
- }
- }
- class Demo {
- public static Object array(Object[] obj,int index)throws ArrayIndexOutOfBoundsException{
- if(index >= obj.length)
- throw new ArrayIndexOutOfBoundsException("下标越界");
- return obj[index];
- }
- }
- public class Test3 {
- public static void main(String[] args)throws ArrayIndexOutOfBoundsException {
- String[] str = {"aa","bb","cc"};
- try {
- System.out.println(Demo.array(str,4));
- } catch (ArrayIndexOutOfBoundsException e) {
- e.printStackTrace();
- }
- }
-
- }
复制代码 各位大虾们可以提供更多的解决方案。谢谢!
|