Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Exception in try block.

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ArithmeticException is a runtime exception. It does not need to be declared.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For me it does throw the ArrayIndexOutOfBoundsException:



Notice that it does not print "Error" nor "Peace".
 
Tejaswini Shirkhedkar
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!
But still not clear
Could you throw a little more light, please?
Thanks

Teju
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic