《java编程思想》读书笔记(13)

本文探讨了Java中子类如何处理父类抛出的异常,包括子类是否必须声明父类已声明的异常,以及如何避免父类异常捕获导致子类异常处理失效的问题。

子类覆写父类的方法时,如果父类的方法进行了异常声明了,子类可以不理会这个声明,不需要进行异常声明。

None.gifpackagecom.vitamin.Console;
None.gif
importjava.lang.Throwable;
None.gif
None.gif
None.gif
publicclassexceptionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)throwsException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODOAuto-generatedmethodstub]
InBlock.gif
derived=newderive();
InBlock.gifd.process();
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
classmyExceptionextendsException
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifmyException()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
classbase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicbase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
publicvoidprocess()throwsmyException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.out.println(
"process()inbaseclass");
InBlock.gif
thrownewmyException();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
None.gif
classderiveextendsbase
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicderive()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

InBlock.gif
publicvoidprocess()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.out.println(
"process()inderivedclass");
InBlock.gif
ExpandedSubBlockEnd.gif}

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

2.如果你先用父类catch掉了异常,那子类异常的catch块就不能到达,编译器就会报错:

None.gifpackagecom.vitamin.Console;
None.gif
importjava.lang.Throwable;
None.gif
None.gif
None.gif
publicclassexceptionTest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
InBlock.gif
publicstaticvoidmain(String[]args)throwsException
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
//TODOAuto-generatedmethodstub]
InBlock.gif
try
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
thrownewexception2();
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(myExceptionex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
ExpandedSubBlockEnd.gif}

InBlock.gif
catch(exception2ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gifSystem.err.println(ex.getMessage().toString());
ExpandedSubBlockEnd.gif}

ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gif
ExpandedBlockEnd.gif}

None.gif
classmyExceptionextendsException
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gifmyException()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif
classexception2extendsmyException
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif
publicexception2()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif
super();
ExpandedSubBlockEnd.gif}

ExpandedBlockEnd.gif}

None.gif

这种用法编译器是会报错的,应该用防止父类屏蔽掉子类异常处理。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值