Hi sir! my doubt is on checked ,unchecked exceptions in java.
1)You know that if there is any possibility for checked exception to be thrown from a method and we do not have intention to handle the thrown exception then we have to declare that exception in method header.That means we have to write
“throws EXCEPTION NAME” in method header.
2)But in case of unchecked exception, if there is any possibility for unchecked exception to be thrown from a method then we do not declare that exception in method header.. That means we do not write “throws EXCEPTION NAME” in method header .
3)Why should we declare the exception in method header in case of checked exceptions ? what benefits are we getting by declaring exception in method header?
4)Why do not we declare the exception in method header in case of unchecked exceptions?
2. Correct, except that you may optionally have the throws "EXCEPTION NAME" if you want.
3. The checked exceptions are intended to be exceptions that may reasonably occur from the method you are calling. Since it is reasonable to expect them, it is helpful as a reminder that you handle the exception in an appropriate way. It is easy to forget that a method may result in an exception, and with no handler, the program may not work as intended.
4. Unchecked exceptions are rare cases that usually won't happen during runtime. They also include exceptions that should only happen due to programming error, like IllegalArgumentException. In these cases a generic error handler will often work as well, or the programmer will fix the bug before release.