You are here:

Java/exception handling


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?

1. Correct
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.  


All Answers

Answers by Expert:

Ask Experts


Artemus Harper


I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.


I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Washington State University

MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2017 All rights reserved.