Results 1 to 18 of 18
Thread: IlllegalArgumentException
- 08-21-2013, 01:32 PM #1
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
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.
- 08-21-2013, 01:37 PM #2
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 8
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
- 08-21-2013, 01:38 PM #3
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
Re: IlllegalArgumentException
Show me a program which throws this exception
- 08-21-2013, 01:38 PM #4
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
- 08-21-2013, 01:46 PM #5
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 8
Re: IlllegalArgumentException
You can use this to validate method arguments. It is one of the most common thrown.
- 08-21-2013, 01:48 PM #6
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
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?
- 08-21-2013, 01:50 PM #7
- 08-21-2013, 01:58 PM #8
Senior Member
- Join Date
- Apr 2013
- Location
- Sweden
- Posts
- 272
- Rep Power
- 8
- 08-21-2013, 02:00 PM #9
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
- 08-21-2013, 02:17 PM #10
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
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.
- 08-21-2013, 02:24 PM #11
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: IlllegalArgumentException
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 08-21-2013, 02:31 PM #12
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
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()); } }
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)
- 08-21-2013, 02:42 PM #13
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-21-2013, 02:46 PM #14
Senior Member
- Join Date
- Aug 2013
- Posts
- 108
- Rep Power
- 0
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?
- 08-21-2013, 03:03 PM #15
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
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
- 08-21-2013, 03:25 PM #16
- 08-21-2013, 03:57 PM #17
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
Re: IlllegalArgumentException
Build a wall around Donald Trump; I'll pay for it.
- 08-21-2013, 05:08 PM #18
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
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,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Bookmarks