tiger0516
asked on
Java Exception
I am a bit confused bu exception.
For example:
(not real code)
try
{
sth.domethod();
print 1;
}
catch (exception a)
{
print 2;
}
catch (exception b)
{
print 3
}
finallu
{
print 4
}
print 5
if there is an expcetion c in sth.domethod(), which is not caught by a or b, will the program print
4
or
45
?
If there is no exception, will it print
145
or
14?
In other words, in which case will the program execute the "print 5" after finally clause and in which case it does not.
Thanks
For example:
(not real code)
try
{
sth.domethod();
print 1;
}
catch (exception a)
{
print 2;
}
catch (exception b)
{
print 3
}
finallu
{
print 4
}
print 5
if there is an expcetion c in sth.domethod(), which is not caught by a or b, will the program print
4
or
45
?
If there is no exception, will it print
145
or
14?
In other words, in which case will the program execute the "print 5" after finally clause and in which case it does not.
Thanks
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry BogoJoker, same timing.
No problem, the fact we both agree is comforting to me and hopefully evidence to tiger that we are both right.
For us both answering the question in 5 minutes, that is a good thing too =)
Joe P
For us both answering the question in 5 minutes, that is a good thing too =)
Joe P
ASKER
Hi Joe, thanks.
Back to my question,
question 1:
if there is another type expcetion,called c in sth.domethod() (my catch clauses, a and b, fails to catach c. in other words, c is an exception which is not a sub/super class of a or b), will the program print
4 or 45 ?
question 2:
If there is no exception at all, will it print
145 or 14?
I think it should be 145.
question 3:
in which case will the program execute after the finally clause and in which case it won't (simply terminate after the finally clause).
Thanks
Back to my question,
question 1:
if there is another type expcetion,called c in sth.domethod() (my catch clauses, a and b, fails to catach c. in other words, c is an exception which is not a sub/super class of a or b), will the program print
4 or 45 ?
question 2:
If there is no exception at all, will it print
145 or 14?
I think it should be 145.
question 3:
in which case will the program execute after the finally clause and in which case it won't (simply terminate after the finally clause).
Thanks
4 should always print
5 should always print
of course if your program goes into an infinite loop, or crashes, then there is a possibility that neither of these will print =)
The finnaly print 4 has 0 affect on the print 5 after the try catch.
Joe P
5 should always print
of course if your program goes into an infinite loop, or crashes, then there is a possibility that neither of these will print =)
The finnaly print 4 has 0 affect on the print 5 after the try catch.
Joe P
ASKER
>5 should always print
So you mean, assuming exception c is a subclass of neither exception a nor exception b, the output of the code if domethod() throws an exception c will be
45 ?
>The finnaly print 4 has 0 affect on the print 5 after the try catch.
So you mean the try-catch-finally mechanism is only good for the try-catch-finally segament? after the finally clause, the code will continue to run anyway?
Many thanks.
So you mean, assuming exception c is a subclass of neither exception a nor exception b, the output of the code if domethod() throws an exception c will be
45 ?
>The finnaly print 4 has 0 affect on the print 5 after the try catch.
So you mean the try-catch-finally mechanism is only good for the try-catch-finally segament? after the finally clause, the code will continue to run anyway?
Many thanks.
Exactly!
All Goes Well: 145
doMethod throws exception a: 245
doMethod throws exception b: 345
doMethod throws exception c: program will crash if not handled, expect no output
Joe P
All Goes Well: 145
doMethod throws exception a: 245
doMethod throws exception b: 345
doMethod throws exception c: program will crash if not handled, expect no output
Joe P
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks a lot. I feel I now understand much more than a few hours ago :)
Sure =)
Joe P
Joe P
Finnally always prints, no matter if an exception is thrown or not.
If an exception is thrown, it will only print the first time it is caught. Then as it should, it will always print the finally.
If your first catch was catch (Exception a) print 2, then the only thing that would ever print would be 2 (if there was an exception). This is because all Exceptions extend Exception, so even if it was an IOException or a NullPointerException, they are both of type Exception, and that print 2 block will work, ignoring ALL the other catch statements.
Then finnaly it would print 4.
So a general rule is to always catch the most specific Exceptions first! =)
Joe P