Results 1 to 4 of 4

Thread: Exception

  1. #1
    jperson is offline Member
    Join Date
    Jan 2010
    Posts
    61
    Rep Power
    0

    Default Exception

    Java Code:
    package test;
    
    public class Circle {
        private  double rad;
        public Circle(){
            this(1);
        }
        public Circle(int x){
            set(x);
        }
        public void set(int x)throws IllegalArgumentException{
            if(x>=0){
            this.rad = x;
            System.out.println(x);
            }
            else
                throw new IllegalArgumentException("radius must be possitive");
        }
    
    }
    in above example what's the benfit of using [throws IllegalArgumentException] ,
    there is no effect if i remove it :confused:

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Whoa, almost misread that one as 'whats the benefit of throwing an IllegalArgumentException.' Must be tired...

    Anyway, its really a documentation thing. For anyone reading your code, the [throws IllegalArgumentException] is an immediate note that the method can throw the aforementioned exception. If you don't have the declaration, someone using the method may not write code that is prepared to handle it in cases where it could be thrown (i.e. when using a value entered by the user).
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  4. #4
    Prajin's Avatar
    Prajin is offline Senior Member
    Join Date
    Jun 2010
    Location
    Ktm, Nepal
    Posts
    120
    Rep Power
    0

    Default

    Quote Originally Posted by jperson View Post
    in above example what's the benfit of using [throws IllegalArgumentException] ,
    there is no effect if i remove it :confused:
    What you did after removing that exception, I guess, run program and pass the integer as argument.
    Why not try to pass except int as argument? Then see the result.

    IllegalArgumentException : - check the validity of method arguments,particularly important for constructors.

    -Regards

Similar Threads

  1. Replies: 4
    Last Post: 10-25-2010, 07:42 PM
  2. get exception name
    By jithan in forum New To Java
    Replies: 6
    Last Post: 06-05-2008, 02:29 PM
  3. exception
    By thamizhisai in forum Advanced Java
    Replies: 9
    Last Post: 05-30-2008, 08:47 AM
  4. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •