sorry,我想问的是,这个程序运行的和我想的不一样为什么?谢谢 我想要的是:C:\>java Ninetyeight Hello World! A B C D 异常抛出 ! 学而实习之! 现在运行的是:C:\>java Ninetyeight Hello World! A B C D 学而实习之! Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at Ninetyeight.<init>(Ninetyeight.java:22) at Ninetyeight.main(Ninetyeight.java:41)
把public Ninetyeight()改一下就可以了 public Ninetyeight() { int i = 0; try{ String DiavArray[] = {"A","B","C","D"}; System.out.println("Hello World!"); while(i<5) { if( i >= 4 ) throw new dlexception(); System.out.println(DiavArray[i]); i++; } } catch(dlexception e) { System.out.println( e ); } finally { System.out.println("学而实习之!"); } }
catch我自己的dlexception为啥不行,他可是ArrayIndexOutOfBoundsException继承来的? 子类不是有父类的方法吗? 是否可以这样理解系统的异常类是自动抛出,自己的类必须throw ? 换句话来说dlexception并不能捕捉到ArrayIndexOutOfBoundsException的异常随然它是ArrayIndexOutOfBoundsException的子类。既然这样从目前我看到的例子,(275、java编程详解)自己写的异常都是在if判断后throw 抛出。从写if ()throw new ×××;还不如写一个System.×××;不就完了吗?实在不行就System.exit();我认为自己写的异常的作用是重用代码,比如异常以后有一大堆的事情要作比如关数据库、、、,可是写一个普通的类作用不也一样吗? 我现在晕了:(
TO: smartboy(天涯一痴人)你的建议只能得到 Hello World! A B C D 学而实习之! Exception in thread "main" 异常抛出 ! at Ninetyeight.<init>(Ninetyeight.java:27) at Ninetyeight.main(Ninetyeight.java:41) 的结果,不符合楼主的意思吧!?
catch(ArrayIndexOutOfBoundsException e) 可以捕捉到所有ArrayIndexOutOfBoundsException的子类异常,如:dlexception的异常 因为,子类对象可以自动的向上转型为父类但是你的程序中 catch( dlexception e ) 捕捉的是ArrayIndexOutOfBoundsException的子类dlexception的异常 而程序抛出的是父类ArrayIndexOutOfBoundsException的异常,父类对象不可能自动向下转型为子类,因此不可以我感觉你似乎对 "向上转型" 和 "向下转型" 不太明白(好像有些书里叫"造型"),本想在这里讲讲,不过讲起来就太长了,建议你看看Thinking in Java,个人认为这本书还是相当不错的,而且侯捷先生翻译的也很好^_^
我的理解 : 自己写的异常不应该继承人具体的异常类如找不到文件,数组越界等等。只能继承于Exception这样的类了。既然这样从目前我看到的例子,(275、java编程详解)自己写的异常都是在if判断后throw 抛出。从写if ()throw new ×××;还不如写一个System.×××;不就完了吗?实在不行就System.exit();我认为自己写的异常的作用是重用代码,比如异常以后有一大堆的事情要作比如关数据库、、、,可是写一个普通的类作用不也一样吗? 我现在晕了:( 谢谢!:)
我想要的是:C:\>java Ninetyeight
Hello World!
A
B
C
D
异常抛出 !
学而实习之!
现在运行的是:C:\>java Ninetyeight
Hello World!
A
B
C
D
学而实习之!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at Ninetyeight.<init>(Ninetyeight.java:22)
at Ninetyeight.main(Ninetyeight.java:41)
改为
catch(ArrayIndexOutOfBoundsException e)//捕获
{
dlexception k=new dlexception();
throw k;//抛出自己定义的异常
}
异常
public Ninetyeight()
{
int i = 0;
try{
String DiavArray[] = {"A","B","C","D"};
System.out.println("Hello World!");
while(i<5)
{
if( i >= 4 )
throw new dlexception();
System.out.println(DiavArray[i]);
i++;
}
}
catch(dlexception e)
{
System.out.println( e );
}
finally
{
System.out.println("学而实习之!");
}
}
子类不是有父类的方法吗?
是否可以这样理解系统的异常类是自动抛出,自己的类必须throw ?
换句话来说dlexception并不能捕捉到ArrayIndexOutOfBoundsException的异常随然它是ArrayIndexOutOfBoundsException的子类。既然这样从目前我看到的例子,(275、java编程详解)自己写的异常都是在if判断后throw 抛出。从写if ()throw new ×××;还不如写一个System.×××;不就完了吗?实在不行就System.exit();我认为自己写的异常的作用是重用代码,比如异常以后有一大堆的事情要作比如关数据库、、、,可是写一个普通的类作用不也一样吗?
我现在晕了:(
Hello World!
A
B
C
D
学而实习之!
Exception in thread "main" 异常抛出 !
at Ninetyeight.<init>(Ninetyeight.java:27)
at Ninetyeight.main(Ninetyeight.java:41)
的结果,不符合楼主的意思吧!?
可以捕捉到所有ArrayIndexOutOfBoundsException的子类异常,如:dlexception的异常
因为,子类对象可以自动的向上转型为父类但是你的程序中
catch( dlexception e )
捕捉的是ArrayIndexOutOfBoundsException的子类dlexception的异常
而程序抛出的是父类ArrayIndexOutOfBoundsException的异常,父类对象不可能自动向下转型为子类,因此不可以我感觉你似乎对 "向上转型" 和 "向下转型" 不太明白(好像有些书里叫"造型"),本想在这里讲讲,不过讲起来就太长了,建议你看看Thinking in Java,个人认为这本书还是相当不错的,而且侯捷先生翻译的也很好^_^
我现在晕了:(
谢谢!:)
是因为你并没有捕获到你的异常,结果中的异常是系统抛出的运行期异常(RUNTIME EXCPETION)ArrayIndexOutOfBoundsException,与转型没有关系的异常的处理不外乎几种可能:
--在本次处理掉,如:
danceflash(Wine)的
……
catch(dlexception e)
{
System.out.println( e );
}
……
--抛出到别处(更适合处理的地方)
如搂主程序中所为
或处理后再抛出至于继承的问题,我个人认为,可以继承,但又有什么用呢——除了把原有异常功能搞乱;从Exception继承自己专用的异常不更好吗?
THINK IN JAVA中关于这块讲的很多,不过我还没看到,呵呵
{
class dlexception extends ArrayIndexOutOfBoundsException
{
public String toString( ){
return "异常抛出 !";
}
}public Ninetyeight() throws dlexception
{
int i = 0;
try{
String DiavArray[] = {"A","B","C","D"};
System.out.println("Hello World!");while(i<5)
{
System.out.println(DiavArray[i]);
i++;
}
}
catch( ArrayIndexOutOfBoundsException e)
{
throw new dlexception();
} finally
{
System.out.println("学而实习之!");
}}
public static void main(String[] args)
{
try{
Ninetyeight d = new Ninetyeight();
}catch(dlexception e)
{
System.out.println(e);
} }
}