Forums Register Login

exception

+Pie Number of slices to send: Send
Can sb. explain it? Thanks in advance.
<code>
code:


1. public class Q13
2. {
3. static int call(int x)
4. {
5. try
6. {
7. System.out.println(x---x/0);
8. return x--;
9. }
10. catch(Exception e)
11. {
12. System.out.println(--x-x%0);
13. return x--;
14. }
15. finally
16. {
17. return x--;
18. }
19. }
20. public static void main(String[] args)
21. {
22. System.out.println(" value = "+ call(5));
23. }
24. }
1 Compile time error.
2 Run time error.
3 Program compiles correctly and prints 4 when executed.
4 Program compiles correctly and prints 3 when executed.


</code>
The answer is 4. i don't understand why line 12 raise an ArithmeticException?
Jason
+Pie Number of slices to send: Send
Hi, for int type, <code>%</code> and <code>/</code> will get ArithmeticException if the dividend is 0. That is the reason line 12 get the exception.
Guoqiao
+Pie Number of slices to send: Send
Hi Jason here's what happens -
in the try block the value of x would be decremented to 4 (--x)before the division by zero .
ArithmeticException thrown by the try block but x ix 4 now .
This Exception caught by the catch block .
Again in the catch block before the modulo by zero , x is decremented to 3 & now once again an ArithmeticException is thrown & x is 3 now .
But since the finally block is gotta be executed anyways instead of throwing the Exception back to the caller it returns 3 ( remember that you have x-- & not --x).
If there were no return statement in your finally block you would've got the Exception .
Hope that helps
Ashish
[This message has been edited by Ashish Hareet (edited July 23, 2001).]
+Pie Number of slices to send: Send
Hi,
I have more questions on this code.
1) Which x-- is returned at last?
2) How does the code handle Line 12 exception?
3) Can anyone give a good site to see the difference between NaN and ArithmeticException?
Thanks.
+Pie Number of slices to send: Send
I got the answers to my first two questions. Just a few minutes late
But should we throw Exception before catching it?
+Pie Number of slices to send: Send
Hi Wei luo,
first the x willbe decremented at line 7 then at line 12 then at the finally block it will be decremented to 3 and prints the answer.
please read ashish answer again.
double x =24.0/0 will give you Not a number
int x = 3/0 will give you Arithmetic exception divide by zero.
tha
Balaji

Originally posted by wei luo:
Hi,
I have more questions on this code.
1) Which x-- is returned at last?
2) How does the code handle Line 12 exception?
3) Can anyone give a good site to see the difference between NaN and ArithmeticException?
Thanks.


+Pie Number of slices to send: Send
Hi, Thank you all.
Ashish, i still didn't get it. line 12 raise an exception, nobody catch it and the program complete normaly. According to what you said, if the finally block complete normaly, we can ignore the exception raised in any catch block, is it right?
Thanks
+Pie Number of slices to send: Send
hi,
the above eg is an interesting question. even i would like to know how come the code runs successfully when an arithmetic exception has risen?
somebody help??
+Pie Number of slices to send: Send
No Jason , the problem here is that you are returning a value in your finally block . Here's how i look at it -
There's only one thing that could be send back to the caller , either an exception or a return value .
A finally clause ensures that the finally block is executed after the try block and any catch block that might be executed, no matter how control leaves the try block or catch block ( JLS 14.19 ).

This means finally has got to be executed no matter what . Since it has got a return clause it will send it back to the caller & now there is no taker left for the exception thrown by the catch so i think it's simply ignored .
Remove the return from the finally block & you'll get the exception .
Take a peek at JLS 14.19.2 it's explained in there but it's rather too cryptic for me .
Or try this , forget the exceptions , simply make your try/catch/finally blocks return some values & see which value is always returned .
Correct me if i am wrong
+Pie Number of slices to send: Send
It sounds like if an exception is raised within catch block it will be handled by finally block and in that case finally block has to return anything to the caller, but for what??? who is the caller here???
--Farooq
+Pie Number of slices to send: Send
Do we need to return only because the method's return type is int.
I tried by commenting the return statements and it compiled.
--Farooq
+Pie Number of slices to send: Send
 

Originally posted by Muhammad Farooq:
Do we need to return only because the method's return type is int.
I tried by commenting the return statements and changing the return type of the method as void, and it compiled.
--Farooq


+Pie Number of slices to send: Send
Hi Farooq,
If your method has a return type, you have to ensure that every block (i.e. try,catch,finally blocks) returns a valid type.Alternatively, you may throw a Exception instead of returning the type.
Hope this helps,
Sandeep
+Pie Number of slices to send: Send
I beg to differ Sandeep .the finally block doesn't need to return anything .It just has to do some finalization like closing filestreams , etc . In fact it is the return type in the finally block that is the causing the confusion . If not for the return in finally we would've got an AritmeticException . And i suggest that people try to compile/run & see what happens when you don't have that return in the finally block .
Hope this helps .
[This message has been edited by Ashish Hareet (edited July 24, 2001).]
+Pie Number of slices to send: Send
Ashish,
I see something more with respect to the return statements in the above code snipplet.
At the outset, I like to mention that there are two ways to exit out of a method, one by using the return keyword and another by throwing an exception.
In the above code snipplet, the return statement in the finally block overrides(may be I am not using the right word! ) the exception thrown in the catch block.When the println() statement in the catch block throws an exception,the control goes to the finally block.The return in the finally block sees to it that the method exits normally without any exception.
However, this doesn't mean that we donot require a return statement in the finally block.Consider the following code:

The above code snipplet is not going to compile unless you have a return statement either at (1), (2) or (3).
In the above code snipplet, the println statement in the catch block is not throwing an exception.So the only way to exit out of this method is by using a return statement.
You may prefer to place this statement at (1),(2) or (3).If you prefer to keep the return statment at (1),when an exception is thrown it will not get executed.The same is the case when you place the return statement at (3).This may result in the method returning a stale value for x; quite likely a value which you didn't expect!

You would most probably want the method to return the value ONLY after all house-keeping options are over.Thus,it makes sense to place the return statement at (2).This will ensure that a proper value is returned after finishing up with the finalizations.
Also, note that above requirement is the LEAST what you should do.If you examine the above code snipplet, we require another return statement even though we already have one in the try block.This means JLS mandates that you should have a normal return statement for every possible exit from the method.So having return statements at (1) and (2) will not harm our cause at all.Also this means that you shouldn't have a return statement at (3), since you have taken care of all the possible exits.
Hope this makes some sense!
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 24, 2001).]
+Pie Number of slices to send: Send
Very well said Sandeep . And now i know that one more possibility could've been a return at the method exit instead of the catch .
Thanx
[This message has been edited by Ashish Hareet (edited July 24, 2001).]
+Pie Number of slices to send: Send
Thanks Sandeep, you have very well expalined it.
--Farooq
+Pie Number of slices to send: Send

I disagree about putting in a return in a finally. See the above code sample. Even though we are throwing an Exception in line 14, we will never see it because of the finally. This may be fine for the person who originally coded the program because they understand what is going on, but this will be a source of confusion to anyone working with this code. It is a bad code habit to place a return in a finally. IMHO.

------------------
Tom - SCJP --- Co-Moderator of the Programmer Certification Forums
+Pie Number of slices to send: Send
Thomas,
It actually depends on what you want your method to do.Since you have specified that the method is going to throw an exception as well as return a value, it is evident that you would program the method in a way which takes care of both the aspects.
Any exit that requires throwing an exception would be handled by a throw clause, whereas any normal exit would be taken care by return statement.
IMO, the catch block is not a very good place to throw an exception.If your method is being declared as throwing an exception, you may not want try..catch..finally block in the method.You may not want to throw an exception in the catch block and make a normal exit (return) from the method using finally block.
The return method should only signify a normal exit from the method.You may want to return a value ONLY AFTER doing all the finalization work.Hence IMO, it makes sense to place the return statement as the last statement of the finally block.Also, it is the safest place to return a value since you usually take care that no exception is raised in the finally block.
Hope this makes sense,
Sandeep
SCJP2,OCSD(Oracle JDeveloper),OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited July 25, 2001).]
Destiny's powerful hand has made the bed of my future. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1166 times.
Similar Threads
on constructor
Unreachable code?
doubt in output of the program
supertype constructor
Runtime Error
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:22:30.