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
LVL 1
tiger0516Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BogoJokerCommented:
Hi tiger0516,

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
0
fargoCommented:
If there is no exception, will it print
145
>>YES

finally block will execute inspite of there is any exception or not.
If there is any exception, the flow will not reach till "print 5".


0
fargoCommented:
Sorry BogoJoker, same timing.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

BogoJokerCommented:
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
0
tiger0516Author Commented:
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
0
BogoJokerCommented:
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
0
tiger0516Author Commented:
>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.
0
BogoJokerCommented:
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
0
BogoJokerCommented:
The kicker is java alerts you when your writing a command that may throw an exeception.
That is like:
BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
in.readLine();

That requires a try catch block.  Just putting that code along will not compile, the compiler will say, you did not catch the possibility of an IOException that may be thrown by readLine().  How does the compiler know that!?  Because readLine() is defined like so:
public String readLine() throws IOException
{ ... }

The trick part is say you make your own function, lets do a simple one like dividing two ints.
public int myFunction(int a, int b)
{
  return (a/b);
}

You can safely do:
int a = 2;
int b = 3;
int c = myFunction(a,b);

But there is the possibility of throwing an exception, you didn't tell the compiler, so this would still blow up and you were never warned that an exception might occur.  For instance you could divide by 0:
int a = 100;
int b = 0;
int c = myFunction(a,b); // inside myFunction it will blow up

So even in a try catch block, if an exception is generated, thrown, and NOT caught, expect the program to blow up because it does not know what to do!




Joe P
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tiger0516Author Commented:
Thanks a lot. I feel I now understand much more than a few hours ago :)
0
BogoJokerCommented:
Sure =)

Joe P
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.