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
Joined: Aug 30, 2000
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