The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2000



This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.


Exceptions v/s Return values

Posted by Amol Deshmukh on October 17, 2000 at 2:42 AM

Pls pardon for a slightly large post.

I have a design related query regarding whether to use
exceptions or return values. I mean how does one decide
or what are the factors that would help one decide whether
to go for Exceptions or Return values.

Sometimes it is not possible to use return values to
indicate error condition like say u return int and all
possible int values are valid return values. Then you go
for throwing exception to indicate an error condition. But
at other times maybe all int values are not valid return
values so that you go for returning a non-standard int value
that indicates an error condition.(Classic example of this
case is the function String.indexOf("somestring") which
returns -1 to indicate search string not found.)

It is such cases where i am not sure whether to go for
using return values or exceptions. Is there any overhead
involved in using exceptions as compared to return values?
Also another factor in my mind that may influence the choice
is the fact that Exceptions have the ability to return more
information wrapped inside the exception object where as
return values cant do that.

I'd like to have a definitive answer on this though, and any
comments on the same would be greatly appreciated.
Amol Deshmukh
Cognizant Technology Solutions,


Sponsored Links

Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us