"Is there any rule like- we need to handle checked exceptions before the completion of the code? "
Joined: Nov 02, 2005
"......completion of the code"? Do you mean compilation of the code?
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.
Narendhiran Nagarajan wrote:I mean, the till the end of stack(Main method).
I see that, we have to handle the checked exceptions.
Yes ,I agree.
Also, I understand that we can throw checked Exceptions fro main method
In which situation
the checked Exceptions are thrown from the main method.? (main method is the last chance to handle the chk excep)
A checked exception should be declared and handled.This means that if a method say m1 declares an exception but does not handle it, it may be handled by m2, m3 0r m4 etc. methods within the same code.This 'passing the buck' situation is called Propagating Uncaught Exceptions.If an exception is not caught even by the main method which is at the bottom of the stack, then compiler issues displays the stack trace, and stops further execution of program.
The following code throws an exception
Joined: Jun 17, 2009
Thanks for all your replies.
But I m sorry to ask the same question again.
Eventhough we can propagate Uncaught checked Exceptions, it should be handled before the main method moves out of the stack.
"So, In which situation the checked Exceptions are thrown from the main method? "