• Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding the semantics of this statement about checked exceptions  RSS feed

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each method must either handle all checked exceptions by supplying a catch clause or
list each unhandled checked exception as a thrown exception.

Sierra, Katherine; Bates, Bert (2008-06-24). SCJP Sun Certified Programmer for Java 6 Study Guide : Exam 310-065 (Page 372). McGraw-Hill Osborne Media. Kindle Edition.


I understand the idea of try-catch-error and propogation but not 100% about checked exceptions. So a checked exception is basically when I choose to throw something? Example:


and an unchecked exception could be the exact same method but we no longer throw anything

 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syed Islam wrote:So a checked exception is basically when I choose to throw something?

No, that's not what makes an exception a checked exception. Whether something is a check or an unchecked exception depends on what the exception class extends.

Any exception that extends java.lang.Error or java.lang.RuntimeException (or their subclasses) is an unchecked exception. All other exceptions are checked exceptions.

This page in Oracle's Java Tutorials explains it: The Catch or Specify Requirement.
 
Syed Islam
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay thanks. I need to ask another question in a different way.

If I create a method, I can add my own "throws" to it.

void methodA() becomes ---------------------> void methodA() throws MyException
then somewhere in that code there is the possibility I can throw my own exception

Likewise there are some Java classes from the IO package that throw exceptions:
Is it possible to get a checked exception from a method that doesn't have 'throws' added to the end? For example, FileNotFoundException is a checked exception. So if we had
without the 'throws' and we input a file name string for a file which didn't exist. What would happen?

 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syed Islam wrote:Is it possible to get a checked exception from a method that doesn't have 'throws' added to the end?

That's answered in the tutorial that Jesper gave you a link to.

Syed Islam wrote:What would happen?

Once you have worked out the answer to the first question, this question becomes moot.
 
Syed Islam
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay you are right this is what I was looking for

But sometimes the user supplies the name of a nonexistent file, and the constructor throws java.io.FileNotFoundException. A well-written program will catch this exception and notify the user of the mistake, possibly prompting for a corrected file name.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!