Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Query regarding extending Exception classes (http://www.programmingforums.org/showthread.php?t=15696)

arj Apr 25th, 2008 5:08 PM

Query regarding extending Exception classes
 
Hi there

I'm doing a small project at home (and possibly thinking of proposing something similar for my final year project at uni) and I found a slight ... thing ... which I'm not too clear about.

I was writing a class (call it MyClass) and found the need to write an Exception class, something I have done many times in the past. The Exception class looked like this:

:

public class MyException extends Exception {
    public MyException(String message) {
        super(message);
    }
}



I then added the line:
:

public MyClass() {
        ...
    else throw new MyException("message");
        ...
}


to the constructor of MyClass. The compiler obviously complained saying that I need a 'throws' clause on the method header which I added (public MyClass() throws MyException), which I added on.

Then I decided to play around a little, so I changed the class declaration of MyException to:
:

public class MyException extends IllegalArgumentException {

(The reason was that in the particular scenario I was trying to capture, MyException was an exception related to illegal paramaters being passed). After doing this I noticed that the compiler no longer asked for a 'throws' clause on the method header which was throwing MyException. :-/ :| I was a bit confused about this, and was wondering if anyone could explain the reasons why this happens?

Thanks,
arj.

andro Apr 25th, 2008 5:31 PM

Re: Query regarding extending Exception classes
 
IllegalArgumentException extends RuntimeException. A method is not required to declare in its throws clause any subclasses of RuntimeException that might be thrown during the execution of the method but not caught.


All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC