Results 1 to 18 of 18
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: IlllegalArgumentException

  1. #1
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default IlllegalArgumentException

    I thought I started a thread which talked about the IllegalArgumentException(other than NullPointerException thread) but I don't seem to find that thread now. So posting it again.

    From the complete reference book:
    An IllegalArgumentException is thrown if an invalid argument is used.
    An invalid argument will not pass through the compiler itself then how will an IllegalArgumentException be thrown?

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: IlllegalArgumentException

    "Thrown to indicate that a method has been passed an illegal or inappropriate argument." From the java doc

    For example you rank, let's say, the priority of a task from 1 to 5 and passing anything else is an illegal argument to the set method for example

  3. #3
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: IlllegalArgumentException

    Show me a program which throws this exception

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default Re: IlllegalArgumentException

    Java Code:
    String in = "1234";
    doIt(in);
    
    // snip
    
    
    public void doIt(String param) throws IllegalArgumentException{
        if(param.length() > 3){
             throw new IllegalArgumentException("Parameter must not be longer than three characters! - " + param);
        }
        // rest of method
    }
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  5. #5
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: IlllegalArgumentException

    You can use this to validate method arguments. It is one of the most common thrown.

  6. #6
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: IlllegalArgumentException

    User defined exception anyone can throw. Show me one program which throws IlllegalArgumentException on its own. An illegal argument will not pass through compiler itself then how will the exception be thrown?

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: IlllegalArgumentException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    User defined exception anyone can throw. Show me one program which throws IlllegalArgumentException on its own. An illegal argument will not pass through compiler itself then how will the exception be thrown?
    Did you read #2?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    262
    Rep Power
    2

    Default Re: IlllegalArgumentException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    User defined exception anyone can throw. Show me one program which throws IlllegalArgumentException on its own. An illegal argument will not pass through compiler itself then how will the exception be thrown?
    What do you mean "on its own" ? It's not a user defined exception, it's a run time exception. Maybe you misunderstood the word illegal.

  9. #9
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: IlllegalArgumentException

    Quote Originally Posted by DarrylBurke View Post
    Did you read #2?

    db
    or better yet, #4. But what it boils down to is - read the other replies before adding your own.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  10. #10
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: IlllegalArgumentException

    I read all the replies. #2 does not have a program example. #4 gives a program in which the IllegalArgumentException is thrown by the user(developer). I want to see a program in which IllegalArgumentException is generated by the system. Better still I want to see code in which the compiler allows the illlegal argument to be passed but at runtime the exception is thrown.

  11. #11
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: IlllegalArgumentException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    I want to see a program in which IllegalArgumentException is generated by the system.
    That makes no sense at all. Of course it is triggered by the developer, 'the system' does not have a mind of its own, it does only what it is instructed to do.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: IlllegalArgumentException

    Ok, lets take this example. Just run the below code and it throws a NoSuchElementException. Similary I want to see an IllegalArgumentException being thrown. Hope you will understand what I am looking for thru the example code pasted below.

    Java Code:
    import java.util.*;
    
    public class QueueDemo
    {
    	public static void main(String arg[])
    	{
    		Queue q=new LinkedList();
    		System.out.println(q.remove());
    	}
    }
    Output:
    Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.remove(Unknown Source)
    at java.util.LinkedList.removeFirst(Unknown Source)
    at java.util.LinkedList.remove(Unknown Source)
    at QueueDemo.main(QueueDemo.java:8)

  13. #13
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: IlllegalArgumentException

    It is still a programmer 'doing it' - the programmer who wrote LinkedList.remove().

    So what you are perhaps REALLY asking for is a standard JDK class that throws an IllegalArgumentException.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    Join Date
    Aug 2013
    Posts
    108
    Rep Power
    0

    Default Re: IlllegalArgumentException

    So what you are perhaps REALLY asking for is a standard JDK class that throws an IllegalArgumentException.
    Ya, so you have an example?

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: IlllegalArgumentException

    Nope. But that doesn't mean there isn't a case. I suggest you yourself download the JDK source and do a code search if you really must know. I really don't care to know to be honest and it is not knowledge I ever needed in the ~10 years I have been professionally programming Java.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: IlllegalArgumentException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    So what you are perhaps REALLY asking for is a standard JDK class that throws an IllegalArgumentException.
    Ya, so you have an example?
    Read the API for IllegalArgumentException
    Click 'Use' at the top of the page
    Read

    db
    gimbal2 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  17. #17
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: IlllegalArgumentException

    Quote Originally Posted by suhaas.mohandos@gmail.com View Post
    Better still I want to see code in which the compiler allows the illlegal argument to be passed but at runtime the exception is thrown.
    Syntactically there's nothing wrong with the argument so the compiler would be happy; it's the semantics of the argument that don' make sense and an IAE will be thrown.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,329
    Rep Power
    5

    Default Re: IlllegalArgumentException

    I am curious though. Why do you care if an example exists in the JDK? Throwing that exception from a method in the JDK is the same as using someone elses class whose method also throws the exception.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Posting Permissions

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