Libraries and API sometimes return -1 or null to indicate an error.
In other cases they generate exception.
If I have to write my own functions, when should I use a return value to indicate an error and when an exception?
Libraries and API sometimes return -1 or null to indicate an error.
In other cases they generate exception.
If I have to write my own functions, when should I use a return value to indicate an error and when an exception?
If you use String.indexOf(...)
, you will see that it returns -1, but this is not an error. Same when consulting a Database, if you don't find what you want, you return null.
But for real errors, real problems, it's better to throw an Exception.
Usually when some method return a value, it's because is an internal method called by anothers and generate exception when are accessible to user them you can handle this exception an don't show an error to user.
EDIT: I ventured unknowingly into java-land.. This answer's code is c#.. sorry about that
Exceptions are expensive (they make your program slow), so they should be used only for really exceptional circumstances. My practice is that most of the time, I return -1 only in my private functions because they don't require as rigorous documentation as others. Also functions that use the format bool TrySomething(in, out result)
is a nice convention if the error appears often.
Then the client can do
if(TrySomething(in, out result))
handle(result);
else
//failed Something
If this is not practical, then generally exceptions are much safer in that they will not give you any silent errors (unless you handle the exception with an empty catch
block).
If it's really an error (the function failed to do what it was supposed to do, because of some internal unexpected error or because the arguments violate some constraint) then you should throw an exception - that's what exceptions are for.
You can return null
or -1 for things like "find the position of a substring inside a given string" to mean "not found", for example. But that's not an error, that's correct and expected ("non exceptional") behaviour.
An exception communicates to the developer (sometimes yourself, often others) that the particular method or function is saying:
I cannot handle the problem and it is someone else's responsibility.
For example, if your method is parsing a string and returning an int, should it return a -1 if it is passed a string like "xyzzy"?
No, -1 would be the wrong error value to return, simply because "-1" is a legitimate string representing a legitimate int.
Here is a case where you would want the parsing method to throw an exception. The calling method would have to handle the parsing exception. You (and future) developers would understand that it's the caller's responsibility to handle a parsing error.
EDIT: In this particular case, you could have the function return an Integer instead of an int. You might then have a contract that null, which is valid for an Integer but not valid for an int, would signify an error of some sort. But because null is a single value, your calling program would not be able to distinguish whether the string were unparsable ("xyzzy") or too big to be represented ("2147483648").