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.
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
Joined: Dec 13, 2002
Thanks Sunita and Ambapali...short and sweet answer...now i have quick idea abt what it is. Thanks Karthik
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
Joined: Oct 03, 2002
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.
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
Joined: Jul 03, 2001
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.