This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Exception handling!!  RSS feed

 
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
 
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.
 
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!