Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
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
Old Apr 25th, 2008, 4:31 PM   #2
andro
Professional Programmer
 
Join Date: Oct 2005
Location: California
Posts: 290
Rep Power: 3 andro is on a distinguished road
Send a message via AIM to andro
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/
andro is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:58 PM.

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