Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception handling!!

 
Ashok Paulraj
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic