Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. Exception Handling

 
Angela Narain
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider the following method :
class MyException extends Exception{}
public void methodA()
{
throw new MyException();
}
I want to clarify three things as follows :
1. If some method called methodA() then it will have to declare
it in the throws clause or put a try-catch block for handling
this exception ( as it is a checked exception ). Am i correct ?
2. How does the following method differ from the above one
public void methodA() throws MyException
{
throw new MyException();
}
3. If the method was throwing RuntimeException( unchecked exception )
then i suppose the method calling it does not need to take
care of handling it as in 1.

 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
1. Yes. Any method invoking methodA() must handle MyException in a try-catch or else declare that it throws MyException.
2. This is how you need to declare methodA(). Your example will not compile correctly if you leave of the throws clause.
3. Right.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
Angela Narain
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jane.
I have one more question :
Consider the below :
class SuperException extends Exception { }
class SubException extends SuperException { }
public void methodA() throws SuperException
{
throw new SubException();
}

Now if some code calls this method does it need to
catch SubException or SuperException ?
 
Angela Narain
Ranch Hand
Posts: 327
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jane , if you u there can you help me with the above one .
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

IMO if another code block calls this method then that code block has to handle the SuperException,i.e. accrding to method signature not runtime type of the exception.
Anyway further clarification is needed....

------------------
azaman
 
Jane Griscti
Ranch Hand
Posts: 3141
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Angela,
Any method invoking <code>methodA()</code>, in your last example, must catch or throw either SubException, SuperException or Exception.
Superclasses of exceptions can always be used; just remember to order your catch statements with the most specific exception first.
For example, if the invoking method had the following try-catch block:

When <code>methodA()</code> throws <code>SubException</code> it will be caught in the <code>SuperException</code> catch block. If you really want to handle <code>SubException</code> seperately from <code>SuperException</code> then you'd need to reverse their order in the catch statements.
If you catch <code>Exception</code> in the first catch block, it will end up trapping any <code>Exception</code> subclass.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic