View Single Post
Old Apr 25th, 2008, 4:08 PM   #1
arj
Newbie
 
Join Date: Apr 2008
Posts: 1
Rep Power: 0 arj is on a distinguished road
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.
arj is offline   Reply With Quote