• 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
This code gives a compiler error because var.m1() throws an Exception which isn't caught or declared.



but this code compiles fine.



why does NumberFormatException not have to be caught or declared?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35279
384
Eclipse IDE Java VI Editor
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kandall,
NumberFormatException is an unchecked/runtime exception. Only checked exceptions need to be handled or declared.
 
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
Hi Kendall,

Don't forget the compiler checks for checked exception in the other way as well. So if you add a try-catch block around some code which doesn't throw the checked exception you mention in the catch block, you'll get a compiler error as well. An example:

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