This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception handling!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception handling!!" Watch "Exception handling!!" New topic
Author

Exception handling!!

Ashok Paulraj
Ranch Hand

Joined: Jul 07, 2003
Posts: 78
Hello All,
clarify catch or declare rule....
public class ExceptionTest extends Exception
{
void method1()
{
method2();
}
void method2() // LINE 1..................
{
try
{
throw new ExceptionTest();
}
catch(ExceptionTest et)
{
System.out.println("EXCEPTION CAUGHT..");
}
}
public static void main(String[] args)
{
ExceptionTest e = new ExceptionTest();
e.method1();
}
}
The above code compiles fine and I get exception caught printed but if I add throws ExceptionTest in Line 1 or with method2(), I get error....why is that so, how do i violate catch or declare rule....I am just trying to declare the exception that the method may throw and also catch the same.
CLARIFY!!!

~ Shalini
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
When you declare a checked exception in a method signature with throws clause, any call to such a method must be either made inside a guarded region (try/catch construct) or the calling method must itself declare the same or broader exception with throws. Hope, this helps.
dhana rangu
Ranch Hand

Joined: Oct 01, 2003
Posts: 65
hi shalini ! let me try to clarify ...


At stmt2 method1() is calling method2();Even thought method2() does not throw any exception the method calling it ie method1() must either declare the exception or throws it since method2() is anouncing that it is throwing Exception. So the correct code will be this way


just try to apply this to your code....Hope this may help u
Ashok Paulraj
Ranch Hand

Joined: Jul 07, 2003
Posts: 78
Thanks Vad & Dhana -- understood the backdrop !!
one more doubt: which is fine.. 1) handling the checked exception in the method it is encountered or being thrown. 2) handle the checked exception in the calling method when the exception is thrown in called method.
coz in the reference books, generally they say, if a method throws a checked exception, it has to be declared and handled in the calling method. Why is that reqd. if I do NO. 1 ??
~ Shalini
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Originally posted by Taurean Lord:
one more doubt: which is fine.. 1) handling the checked exception in the method it is encountered or being thrown. 2) handle the checked exception in the calling method when the exception is thrown in called method.
coz in the reference books, generally they say, if a method throws a checked exception, it has to be declared and handled in the calling method. Why is that reqd. if I do NO. 1 ??
~ Shalini

If you know that an exception thrown in your method is dealt with right away (try/catch), you normally don't want to declare it in the method signature. Unless, of course, you rethrow it again up the stack. Or, if you forsee your method being overridden in the future, you can specify some checked exception that you think will be relevant. This is very handy especially for interfaces and their implementers. For instance, interface javax.servlet.Servlet declares method service() that throws java.io.IOException and javax.servlet.ServletException. Class HttpServlet implements Servlet and overrides service() method. When you create a servlet, you normally extend HttpServlet class and override service() method if needed. The actions inside service() might throw the exception types specified in the interface method declaration.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Exception handling!!
 
Similar Threads
Runtime exceptions??
The throw question
exception problem:looking at following two code fragments!
Is finally, really finally
Exception test