aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how do I know a kind of Exception is a Checked Exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how do I know a kind of Exception is a Checked Exception?" Watch "how do I know a kind of Exception is a Checked Exception?" New topic
Author

how do I know a kind of Exception is a Checked Exception?

Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
For example,

InterruptedException?
Taihua Yu
Greenhorn

Joined: Oct 27, 2006
Posts: 7
The more you see the java doc, the better you'll understand ,most of all ,practice is much more important!
Krishna Srinivasan
Ranch Hand

Joined: Jul 28, 2003
Posts: 1844

http://java.sun.com/docs/books/jls/second_edition/html/exceptions.doc.html

except from sun spec:

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (�8.4.4) or constructor (�8.8.4) must mention the class of that exception or one of the superclasses of the class of that exception. This compile-time checking for the presence of exception handlers is designed to reduce the number of exceptions which are not properly handled.

The unchecked exceptions classes are the class RuntimeException and its subclasses, and the class Error and its subclasses. All other exception classes are checked exception classes. The Java API defines a number of exception classes, both checked and unchecked. Additional exception classes, both checked and unchecked, may be declared by programmers. See �11.5 for a description of the exception class hierarchy and some of the exception classes defined by the Java API and Java virtual machine.

The checked exception classes named in the throws clause are part of the contract between the implementor and user of the method or constructor. The throws clause of an overriding method may not specify that this method will result in throwing any checked exception which the overridden method is not permitted, by its throws clause, to throw. When interfaces are involved, more than one method declaration may be overridden by a single overriding declaration. In this case, the overriding declaration must have a throws clause that is compatible with all the overridden declarations (�9.4).

Static initializers (�8.7), class variable initializers, and instance initializers or instance variable initializers within named classes and interfaces (�8.3.2), must not result in a checked exception; if one does, a compile-time error occurs. No such restriction applies to instance initializers or instance variable initializers within anonymous classes (�15.9.5).


Krishna Srinivasan
Spring Tutorials, OCAJP Mock Questions, 400+ OCPJP Mock Questions
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9350
    
    2

The best would be to have a look at the Throwable class and it's sub classes.


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
For the exam you should only know that
InterruptedException and any
IOException
are Checked Exceptions.


I wanted to ask this question too, but I have this guess,

Runtime vs Checked Exceptions, I don't talk about errors

1- Runtime Exceptions are always 100% programmer fault, like casting nullPoniter, indexOutBounds, IllegalMonitorStateException, ....extra

2- while, checked Exceptions are likly to happen without programmer fault, or with another programmer fault, for example

-InterruptedException could be thrown if another programmer who are
using your Thread(s), makes an interrupt on a thread while this
thread is sleeping.

-FileNotFoundException is not programmer fault.

Is that make any sense to you ?


SCJP 5
Steven Gao Song
Ranch Hand

Joined: Oct 02, 2006
Posts: 78
We can check it out from JAVA API

--------------------------------------------------
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.InterruptedException

--------------------------------------------------

So, InterruptedException is a checked exception,
however,
--------------------------------------------------
java.lang.Object
java.lang.Throwable
java.lang.Exception
java.lang.RuntimeException
java.lang.NullPointerException
--------------------------------------------------

All the RuntimeExceptions are inherited from java.lang.RuntimeException

I draw a conclusion that to judge an exception is checked or unchecked,
just see its family tree.

Correct me if I am not correct.
Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Yes you are 100% correct,

I thought that you want to know which are checked exceptions without looking to any trees.

This information
"
All Runtime Exception are extending RuntimeException is stated explicitly
in every book and tutorial talk about exceptions.
"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how do I know a kind of Exception is a Checked Exception?