• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring thrown exceptions

 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I right in thinking you can declare any exception or error in a method (assuming you import any needed packages) even if there is no possibility of the error or exception happening in the method? I know it would be bad programming form, but I might need to know for the test.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did the compiler tell you if you compiled a little code snippet with an empty method which declares a few (checked/unchecked) exceptions?
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It didn't complain which is why I think it is ok.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:It didn't complain which is why I think it is ok.

Spot-on!

Note: it's different with checked exceptions in a try-catch block...
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note: it's different with checked exceptions in a try-catch block..


How is it different?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:
Note: it's different with checked exceptions in a try-catch block..


How is it different?

When you have a catch handler for a checked exception which isn't thrown in the try-block, you'll get a compiler error. Example:
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:
Kendall Ponder wrote:
Note: it's different with checked exceptions in a try-catch block..


How is it different?

When you have a catch handler for a checked exception which isn't thrown in the try-block, you'll get a compiler error. Example:


That implies to me that Exception is not a checked exception but the following code doesn't compile which implies to me Exception is a checked exception. What am I missing?


Note this code does compile because RuntimeException is not a checked exception.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kendall Ponder wrote:That implies to me that Exception is not a checked exception but the following code doesn't compile which implies to me Exception is a checked exception. What am I missing?

Not true! Unchecked exceptions = RuntimeException, Error, and their subclasses. So Exception is definitely a checked exception, and so is Throwable as well. Illustrated in this code snippet:Both methods do not compile, because Exception respectively Throwable are not handled nor declared.

The reason why you can have a catch-handler for Exception in a try/catch-block even if no exception is thrown from the try-block, is because you can use Exception (and Throwable as well) to catch unchecked exceptions. And because these are not checked by the compiler, you don't get a compiler error. As shown in this code snippet:Even without the code in the try-block this code compiles without errors.

Hope it helps!
Kind regards,
Roel
 
Kendall Ponder
Ranch Hand
Posts: 205
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic