• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Exceptions [Try/Catch]

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i came across this problem from a java text book from topics of exceptions (unfortunately the text book dosn't have solutions so i cant check the answers)
however i have made an attempt at this questions below at the bottom of the page..Correct me if i'm wrong ..thanks

in the following code, the method method1() produces no output on the screen and has no return value.




My Answers below

Questions:
what is the output on the screen when method1()...

1. executes normally?
before finally
2.throws a NullPointerException?
before finally
3.throws an IllegalArgumentException?
IAE finally
4.throws an IOException?
before finally


Thanks
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately, neither of them correct. May be you could explain how you came up with your answers and we could tell you what went wrong.
Actually you could get the answer by testing on a real java program by implementing method1().
 
bruce tidehunter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Raymond Tong wrote:Unfortunately, neither of them correct. May be you could explain how you came up with your answers and we could tell you what went wrong.
Actually you could get the answer by testing on a real java program by implementing method1().


just going through the code and understanding how the code will respond..just going through it again

1. executes normally?
before after finally (assuming that no exceptions are thrown and finally clause is always executed)
2.throws a NullPointerException?
before RE finally (looking at the exceptions hierarchy nullpointerExceptions is a subclass of runtimeException hence it skips the IAE)
3.throws an IllegalArgumentException?
before IAE finally (assuming that runtimeException is not thrown since IAE is a subclass)
4.throws an IOException?
before finally (unsure about this one as the IOException is a direct subclass of the exception class)
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've now got 1, 2 and 3 correct, but not number 4.

IOException is a subclass of Exception (but not of RuntimeException or IllegalArgumentException). So where is an IOException handled, and what gets printed?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To add to what Jesper said: If you look in the documentation for IOException (or any class), you will find its inheritance hierarchy displayed at the top left of the page.
 
bruce tidehunter
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:You've now got 1, 2 and 3 correct, but not number 4.

IOException is a subclass of Exception (but not of RuntimeException or IllegalArgumentException). So where is an IOException handled, and what gets printed?



4. before E finally (since it is a subclass of Exception)
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

bruce tidehunter wrote:4. before E finally (since it is a subclass of Exception)


Correct!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At the risk of having to move myself to MD, surely the output is “I before E except after C”?
 
reply
    Bookmark Topic Watch Topic
  • New Topic