![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Apr 2008
Posts: 1
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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.
__________________
http://www.kevinherron.com/ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| exception handling | l2u | C++ | 2 | Mar 3rd, 2007 5:47 PM |
| coldfire assembly - rts causes Illegal Instruction Exception | mika | Assembly | 4 | Jun 18th, 2006 3:35 PM |
| Could some please explain classes to me... | TCStyle | C++ | 10 | Feb 20th, 2006 3:51 PM |
| AhhH!!! I can't find anything on this exception... | stakeknife | ASP | 2 | Sep 26th, 2005 7:48 AM |
| C++ Classes - Quick question(s) | Delete | C++ | 8 | Apr 7th, 2005 7:38 PM |