Java: catching specific exceptions

Say I have the following

try{
//something
}catch(Exception generic){
//catch all
}catch(SpecificException se){
//catch specific exception only
}

What happens when a specificexception is encountered? Does it catch it first as a generic exception and then a specificexception?

Or it captures only specificexceptions and ignores generic exceptions

I don't want to catch generics and tricky exceptions

Solution

No, all exceptions are caught by the first block The second will never be achieved (compiler recognition, resulting in errors due to unreachable code) If you want to deal specifically with specificexceptions, you must do this:

}catch(SpecificException se){
//catch specific exception only
}catch(Exception generic){
//catch all
}

Then the specificexception will be captured by the first block and everything else will be captured by the second block

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>