This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reg. Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reg. Exception Handling" Watch "Reg. Exception Handling" New topic
Author

Reg. Exception Handling

Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Angela Narain
Ranch Hand

Joined: Apr 14, 2001
Posts: 327
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

Joined: Apr 14, 2001
Posts: 327
Jane , if you u there can you help me with the above one .
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370


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


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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
 
subject: Reg. Exception Handling
 
Similar Threads
"Handle or Declare" Rule for Checked Exceptions
Jxam Exceptions question
exceptions - handle or declare rule
Reg. Exception hierarchy
Questions on naming convention