Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Java Exception

Posted on 2006-04-16
11
Medium Priority
?
291 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:tiger0516
  • 6
  • 3
  • 2
11 Comments
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16465688
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
 
LVL 11

Assisted Solution

by:fargo
fargo earned 200 total points
ID: 16465701
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
 
LVL 11

Expert Comment

by:fargo
ID: 16465708
Sorry BogoJoker, same timing.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 17

Expert Comment

by:BogoJoker
ID: 16465721
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
 
LVL 1

Author Comment

by:tiger0516
ID: 16465744
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
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16465765
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
 
LVL 1

Author Comment

by:tiger0516
ID: 16465813
>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
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16465834
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
 
LVL 17

Accepted Solution

by:
BogoJoker earned 1000 total points
ID: 16465858
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
 
LVL 1

Author Comment

by:tiger0516
ID: 16465926
Thanks a lot. I feel I now understand much more than a few hours ago :)
0
 
LVL 17

Expert Comment

by:BogoJoker
ID: 16465932
Sure =)

Joe P
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question