It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Pls help me on Checked Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Pls help me on Checked Exceptions" Watch "Pls help me on Checked Exceptions" New topic
Author

Pls help me on Checked Exceptions

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Hi...
I could not understand the concept of checked exceptions? Can anyone explain me briefly what is it?where it can be used? how it is to be used?
Thanks in advance.
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Checked exceptions are those that the compiler checks.. if there is a probability that a block of code could throw a kind of checked exception it should be having a catch block with that exception.. otherwise the compiler will complain..
Hope this helps.


Sunita<br />SCJP 1.4
Ambapali Pal
Ranch Hand

Joined: Dec 17, 2002
Posts: 47
Checked Exception are those which are subclass of Exception but not RuntimeException and subclass of RuntimeException.
Sunita is right that Checked Exceptions are checked by compiler for handling the exception either by try/catch block or throws clause. That is if a method may through a checked exception it may handle it inside the method using try/catch block or it can use a throws clause for its caller method to handle the exception.
Ambapali
Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Thanks Sunita and Ambapali...short and sweet answer...now i have quick idea abt what it is.
Thanks
Karthik
bhatti mansha
Greenhorn

Joined: Jan 03, 2003
Posts: 7
is exception is not a run time error
or it is cheaked by JVM
i hope it is right
kay if exception is create or a object i create
and object go to Exception class and find his sub classes
but you al of r talking abt compiler
is complier also cheak exception
plz help me in it. I m student
Faheem
sun par
Ranch Hand

Joined: Oct 03, 2002
Posts: 257
Checked exceptions are different from runtime exceptions or unchecked exceptions. If you dont catch runtime exceptions the compiler does not complain.. It is because it is not expected in the normal execution of the program...
Hope this helps.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch bhatti.
Runtime exceptions are supposed to represent programmers bugs. For instance accessing a non existing array element -ArrayIndexOutOfBoundException- , or calling a method with a non valid argument -IllegalArgumentException-
Because they are possible to ocurr at so many places in the code, it is not feasible that all the expressions possibly throwing them are to be placed within a try clause. Besides, given that they represent programmer errors, the programmer itself should round them in a try clause if they were checked ones. But, if the programmer knew that a runtine exception (programmer error) is going to result from an expression, why placing it within a try clause?, Just correct the expression to avoid it.
Checked exceptions, on the other hand, represent non programmers errors, for which is valid to try to recover. Tipically accessing IO could fail in a file. The programmer is obliged for the compiler to manage the possibility of failure; either in a inmediately enclosing try/catch clause, or by declaring the exception to be thrown, in a calling method of the method where the exception was thrown.


SCJP2. Please Indent your code using UBB Code
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
On the other thought, the compiler does not force the programmer to manage the possibility of failure. Because it is possible simply to declare the exceptions in all the methods participating in the call stack. The compiler merely forces the programmer to be aware of what kind of exceptions could be thrown. However the recommendation is to catch the exceptions in a context in which enough information exists for recovering.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Pls help me on Checked Exceptions
 
Similar Threads
Checked and Unchecked Exceptions
checked and unchecked exceptions
exceptions
Use of Checked and Unchecked Exception
Exception question from Dan