Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checked vs unchecked exceptions

 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand, a checked exception is an exception declared by a "throws" statement. And this can be any throwable exception. The code itself should only throw exceptions that are introduced by something outside the scope of the code such as a user creating a "division by zero condition" or trying to open a file that doesn't exsist.
 
Marine
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so what exactly is your question ? You are right checked exceptions are the once that are declared in throws clause. It becomes more interesting when you sublass a class which has a method that has checked exceptions declared. Now when you override that method you cannot declare any other exceptions in a trows clause because a superclass does not allow you to do so. But if you overload that method you are allowed to add as many as you'd like. I don't know if this helps you in any way.
 
Kevin Cary
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.
I was getting confused as to what exactly are "checked exceptions." After looking at a different book I realize that "Runtime" and "Error" exceptions are not "checked exceptions" which is to say that they are not checked at compile time. Your comments regarding checked exceptions in relation to overloading and overriding are a help since this is an area that is new to me.
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait a minute. I think we are still a little confused here.
Checked exceptions are exceptions that you have to account for in your code. You can either throw them with a throws clause in your method name, or you can catch them in a try..catch block. But the compiler makes sure you do one or the other.
Runtime exceptions or Error are things you don't have to account for. You can declare them in the throws or try..catch block, but the compiler doesn't force you to do so.
I was getting a little worried when all you were talking about is throws statement, because many checked exceptions are handled in try..catch blocks.
As for the rules on overriding, remember that this rule only applies for checked exceptions. The rule is if you override a method in a subclass, then you cannot throw any CHECKED excpetions that were not handled in the superclass. So if the superclass throws IOException, in the subclass you can throw IOException and you can also throw any subclasses of IOException like FileNotFoundException becuase that exception was handled in the superclass. However you cannot throw class Exception because that is the superclass to IOException and was not handled in the superclass. Also, you can throw any RuntimeException you want in the subclass's overridden method and you can also throw no exception in the subclass's overridden method.
Hope that makes sense. The above wasn't the full story so I wanted to make sure you got it.
Bill
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I'm going to move this over to Certification Study
------------------
Jane Griscti
Sun Certified Java 2 Programmer
"When ideas fail, words come in very handy" -- Goethe
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic