![]() |
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 {I then added the line: :
public MyClass() {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. |
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