GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes 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 "Exception" Watch "Exception" New topic
Author

Exception

raj yagnik
Greenhorn

Joined: Feb 14, 2001
Posts: 15
What is Check Exception & Run Time Exception?
Shetty
Greenhorn

Joined: May 21, 2001
Posts: 4
Hi Raj,
Exceptions r of 2 types checked or unchecked. Except for RuntimeException,Error & their subclasses all exceptions r checked. Exceptions defined by Error & RuntimeException classes & their subclasses r called unchecked.
The compiler ensures that if a method or a constructor can throw a checked exception then it must either catch the exception or pass it on to its caller. Since its been chekced at compile time hence the name checked exceptions.
Where as for unchecked exceptions the compiler doesnot complain even if its not taken care of ( i.e no try-catch or throws clause). The reason for not checking it is RuntimeException can normally be avoided if the program is written correctly. Hence it should not arise in a correct program, so u r not required to handle it.
Kiran Shetty
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi raj, Kiran,
Just an FYI:

The reason for not checking it is RuntimeException can normally be avoided if the program is written correctly. Hence it should not arise in a correct program, so u r not required to handle it.

That's somewhat misleading RuntimeExcepetions are those than can occur anytime, anywhere.

JLS §11.2.1 Why Errors are Not Checked
Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. A program declaring such exceptions would be cluttered, pointlessly.
11.2.2 Why Runtime Exceptions are Not Checked
The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs. Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer.
Requiring such exception classes to be declared would simply be an irritation to programmers.

Ideally, they should not occur. You can still catch and handle them in your code if you know one ie ArthmeticException will be generated.
Please read the JavaRanch Name Policy and re-register using a name that complies with the rules.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Kiran GShetty
Greenhorn

Joined: May 25, 2001
Posts: 2
Hi Jane,
You r right about the why RuntimeException & Errors r not checked at compile time. Thanks for the information.
Kiran GShetty.
raj yagnik
Greenhorn

Joined: Feb 14, 2001
Posts: 15
Can someone tell me
checked exception will check while compile time or runtime?/
and unchecked exception will check while compile time or runtime?
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Hi,
Checked Exceptions are checked during Compile time.
Unchecked Exceptions are checked during Run time.
Please correct me if I am wrong.
Jane's quote from JLS is very informative.
Vanitha.
raj yagnik
Greenhorn

Joined: Feb 14, 2001
Posts: 15
hi,
u are right.
raj yagnik
 
GeeCON Prague 2014
 
subject: Exception