Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Excepting-throwing code

Thread view: 
Alper - 12 Oct 2006 00:54 GMT
I have a question about exceptions..

I have this method;

public void calculate(int x) throws IllegalArgumentException {

if(x > 10) throw new IllegalArgumentException("x must be bigger than
10);

//some code here

}

and this one;

public calculate(int x) {

if(x > 10) throw new IllegalArgumentException("x must be bigger than
10);

//some code here

}

What's the purpose of putting "throws IllegalArgumentException" in the
method defination? I mean I run both of them, I couldn't see any
difference.. both gives the same error when I enter argument smaller
than 10. I know it is declaring to the method (which calls calculate())
that it will throw exception.. but it also runs without it.. so why
would I bother putting it?

Thanks for ur answers..
Manish Pandit - 12 Oct 2006 01:55 GMT
Hi,

Assuming you are referring to java.lang.IllegalArgumentException, it
will not make a difference because IllegalArgumentException is an
unchecked exception (it extends java.lang.RuntimeException). You do not
need to declare unchecked exceptions in the method signature per the
standard practices. They do need to be mentioned in the javadocs
though, so that the consumers know what to expect.

This exception falls under the same category as NullPointerException -
you can specify 'throws NullPointerException' in your method signature,
but the compiler will not force the consumers to handle it. Similarly,
you can throw NullPointerException at will from your code, the compiler
will not force you to declare it.

-cheers,
Manish


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.