• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Exceptions [Try/Catch]  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • 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
  • 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: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bruce tidehunter wrote:4. before E finally (since it is a subclass of Exception)

Correct!
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • 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”?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!