Results 1 to 4 of 4
Thread: Exception
- 08-17-2010, 11:18 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 61
- Rep Power
- 0
Exception
in above example what's the benfit of using [throws IllegalArgumentException] ,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"); } }
there is no effect if i remove it :confused:
-
Have a look at the tutorial on unchecked exceptions: Unchecked Exceptions The Controversy (The Java™ Tutorials > Essential Classes > Exceptions)
The rest of the tutorial is pretty decent too.
Luck.
- 08-17-2010, 11:27 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
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!
- 08-18-2010, 08:38 AM #4
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
-
Exception in thread "main" java.lang Exception In InitializerError
By kenzo2009 in forum New To JavaReplies: 4Last Post: 10-25-2010, 07:42 PM -
get exception name
By jithan in forum New To JavaReplies: 6Last Post: 06-05-2008, 02:29 PM -
exception
By thamizhisai in forum Advanced JavaReplies: 9Last Post: 05-30-2008, 08:47 AM -
Where does the exception go?
By aytidaalkuhs in forum New To JavaReplies: 3Last Post: 04-07-2008, 02:24 PM -
Trouble with factory method - unhandled exception type Exception
By desmond5 in forum New To JavaReplies: 1Last Post: 03-08-2008, 06:41 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks