• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need some Explanation - Mala Gupta OCA book Chapter 7, Question: 7-7

 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Referring to Mala Gupta OCA book Chapter 7, Question: 7-7, it says that

The following is an example of a try-catch
block that won’t compile because it tries to catch a checked exception that’s never
thrown by the try block:



So, what I am understanding from this is, you cant place a checked exception in catch argument unless Try block must throw it. How is that , kindly explain?

Kaleem
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaleem Anwar wrote:

So, what I am understanding from this is, you cant place a checked exception in catch argument unless Try block must throw it. How is that , kindly explain?

Your understanding is correct. You cannot catch an exception that is not thrown within the associated try block.
Why? Well, there would not be any point having code there to catch an exception that can never be thrown. The code would be redundant and the compiler is smart enough to tell you so you can remove it.
Redundant code is not desirable. Every line of code adds a risk of bugs and carries a maintenance overhead. And in principle every code path should be tested, so a redundant code path would require adding a redundant test!
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Steffe. I try to compile below code and it compiled successfully.Any thoughts??





Kaleem
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is strange Kaleem because it doesn't compile here, I get:

 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:You cannot catch an exception that is not thrown within the associated try block.

Not 100% correct! You cannot catch a checked exception that is not thrown from the try-block. You can add catch-blocks for any runtime exception you want, even if it's not thrown from try-block.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaleem Anwar wrote:I try to compile below code and it compiled successfully.Any thoughts??

IndexOutOfBoundsException is not a checked exception, but a runtime exception. Only checked exceptions (= subclasses from java.lang.Exception but not subclass of java.lang.RuntimeException) will result in a compiler error.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:That is strange Kaleem because it doesn't compile here

You are trying to compile the code snippet from the original post, but the second code snippet (which compiles successfully) has a runtime exception and not a checked exception (like java.io.FileNotFoundException)
 
Kaleem Anwar
Ranch Hand
Posts: 40
2
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roel and Steffe, now i understand this.
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:You cannot catch a checked exception that is not thrown from the try-block. You can add catch-blocks for any runtime exception you want, even if it's not thrown from try-block.

Ah yes, I had forgotten about that distinction, thanks. This is because we would not want the compiler to tell us about missing catch's for things like OutOfMemoryError, StackOverflowError or NPE which we would have to cover everywhere in our code.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One important note: although the class java.lang.Exception is a checked exception, this code snippet will compile successfullyWhy? Because using a catch-handler for java.lang.Exception you can catch runtime exceptions as well (as it's the parent class of java.lang.RuntimeException).

And here's a code snippet to prove java.lang.Exception is really a checked exceptionIf you try to compile this code snippet, you'll get a compiler error on line1 because java.lang.Exception is not handled nor declared.

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic