File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception in try block. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception in try block." Watch "Exception in try block." New topic
Author

Exception in try block.

Tejaswini Shirkhedkar
Greenhorn

Joined: Jun 22, 2004
Posts: 17
Hi,
This is the question I came across in one of mock exam sites.

Options :

1 Compiler error , return "Error" : statement not reached

2 Compiler error , i = 1 / 0 : Exception must be caught or declared in the throws clause

3 Compiles & prints Error when run

4 Compiles & prints Peace when run

The answer given is option 4. I did compile and run the program and it gives the same output.
I didn't understand the fact that:
In this case, a catch block is throwing an ArithmeticException.This exception is unhandled, then why does it not, either (1) get propogated to the caller?( method main in this case) or (2) give compile time error making the answer option 2 ?
Any explainations?
Thanks

Teju

[ June 27, 2004: Message edited by: Tejaswini Shirkhedkar ]

([C0DE] [/C0DE] tags added - Barry)
[ June 27, 2004: Message edited by: Barry Gaunt ]
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
ArithmeticException is a runtime exception. It does not need to be declared.
Gerome Kawa
Ranch Hand

Joined: Dec 05, 2002
Posts: 61
Option 4 : prints peace is correct because
- Arithmetic exception is a runtime exception , so won't be caught during compile time.
- finally will always be called !

Hope the explanation helps.
Tejaswini Shirkhedkar
Greenhorn

Joined: Jun 22, 2004
Posts: 17
Thanks a lot for your replies..
But then, in the output, it should show that java.lang.ArithmeticException etc etc etc..
But the output is just "peace"
Does this mean that the ArithmeticException getting thrown in the catch block is ignored at run time?
Thanks.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
For me it does throw the ArrayIndexOutOfBoundsException:



Notice that it does not print "Error" nor "Peace".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Tejaswini Shirkhedkar
Greenhorn

Joined: Jun 22, 2004
Posts: 17
Thanks!!
But still not clear
Could you throw a little more light, please?
Thanks

Teju
Anil Hulikal
Greenhorn

Joined: Apr 02, 2004
Posts: 29
I think your return statement in the finally block is playing a role here. Please try replacing that with a print statement (System.out.println ("peace")). Khalid and Rasmussen's book has the following explanation on the use of transfer statements in finally block:

***
If the finally block executes a control transfer statement such as, a return or a labeled break, then this control transfer statement determines how the execution will proceed´┐Żregardless of how the try block or any catch block were executed. In particular, a value returned by a return statement in the finally block will supersede any value returned by a return statement in the try block or a catch block.
***

I wonder if this example is set to demonstrate this point.

Nevertheless, got stumped


Just stay focused.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
I got the following warning while compiling the code




(edited: executing -> compiling -Barry)
[ June 28, 2004: Message edited by: Barry Gaunt ]
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
That's correct Anshul, for some reason I did not notice that myself. This problem was discussed a few days ago in this forum. Here it is.

Another is this one.
[ June 28, 2004: Message edited by: Barry Gaunt ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in try block.