• Post Reply Bookmark Topic Watch Topic
  • New Topic

ParseException vs Exception  RSS feed

 
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have a doubt regarding the following code snippet:



Doubt: If I uncomment Line 2 (and comment Line 3) it gives compile time error stating that ParseException is never thrown.
In order to compile it successfully I have to uncomment Line 1.

However if we run it as is, the program compiles and run successfully.

Please advise why is this so?

Thanks
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiler is good enough to decipher that no ParseException is thrown in the try block and hence gives a compiler error. The Exception class and all RuntimeExceptions are exemptions to this rule.

Hence below is valid.


But it's not valid for other Checked Exceptions.
 
Kaur Manpreet
Ranch Hand
Posts: 30
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please correct me ... for checked exceptions we have the declare or handle rule, so the reverse is also true... i mean not sure to call it reverse... if there is no exception then compiler would not allow us to handle or declare these exceptions too.

Thanks
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By declare I guess you mention throws clause in the method declaration? If so it's valid to have Exceptions that are not thrown to be in the throws clause like below.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kaur Manpreet wrote:Please correct me ... for checked exceptions we have the declare or handle rule, so the reverse is also true... i mean not sure to call it reverse... if there is no exception then compiler would not allow us to handle or declare these exceptions too.

Thanks


This is true. In cases where the compiler can determine neither that exception X nor any of its subclasses can be thrown, it won't allow you to catch X or declare that you throw X. However, since unchecked exceptions (RuntimeException, Error, and their descendants) don't have to be declared, they can be thrown anywhere, and the compiler can't tell whether they might be thrown or not, so it always allows you to catch or declare them.

And, since RuntimeException is a child of Exception, if always allows you to catch Exception, since that will include RuntimeException and its descendants.
 
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:This is true. In cases where the compiler can determine neither that exception X nor any of its subclasses can be thrown, it won't allow you to catch X or declare that you throw X.

Compiler will actually allow you to declare that a checked exception is throw even if it isn't, ie. this compiles (as has been already pointed out by others):

The reason probably is that the throws clause is part of the method's contract, and if you change the declaration of the method to throw checked exceptions previously not thrown, you break the contract. Therefore, if you plan a method will throw a checked exception eventually, but don't implement it now, you can still declare it so that the contract won't get broken when the implementation is finished.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Martin Vajsar wrote:
Jeff Verdegan wrote:This is true. In cases where the compiler can determine neither that exception X nor any of its subclasses can be thrown, it won't allow you to catch X or declare that you throw X.

Compiler will actually allow you to declare that a checked exception is throw even if it isn't, ie. this compiles (as has been already pointed out by others):


Oops. My bad. I stand corrected.

You can declare that you throw an exception that you don't actually throw; you just can't catch one that is known not to be thrown.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!