I'll explain what I mean by input error checking.
Say you have a function doSomething(x)
.
If the function completes successfully doSomething
does something and returns nothing. However, if there are errors I'd like to be notified. That is what I mean by error checking.
I'm looking for, in general, the best way to check for errors. I've thought of the following solutions, each with a potential problem.
Flag error checking. If doSomething(x)
completes successfully return null
. Otherwise, it returns a boolean or an error string. Problem: Side effects.
Throwing an exception. Throw an exception if doSomething(x)
encounters an error. Problem: If you are performing error checking for parameters only, throwing an IllegalArgumentException
seems inappropriate.
Validating input prior to function call. If the error checking is only meant for the parameters of the function, then you can call a validator function before calling the doSomething(x)
function. Problem: What if a client of the class forgets to call the validator function before calling doSomething(x)
?
I often encounter this problem and any help or a point in the right direction would be much appreciated.
If something goes wrong, Java will jump to the catch block. It checks what you have between the round brackets to see if you have handled the error. If you have the correct Exception type then whatever code you have between the curly brackets of catch will get executed.
One common example of the error is when the Java virtual machine (JVM) runs out of memory, which will throw an OutOfMemoryError. Runtime - runtime exceptions are internal to your application but are not typically recoverable.
Definition: An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. When an error occurs within a method, the method creates an object and hands it off to the runtime system.
In Object-Oriented Programming (OOP), exceptions are a powerful mechanism for centralized processing of errors and exceptional situations. This mechanism replaces the procedure-oriented method of error handling in which each function returns a code indicating an error or a successful execution.
Throw an exception is the best way.
If you are performing error checking for parameters only, throwing an IllegalArgumentException seems inappropriate.
Why? That's the purpose of this Exception.
- Flag error checking
This is appropriate in some cases, depending on what you mean by an "error".
An example from the API: If you try to add an object to a Set
, which already contains another object which equals
the new object, the add
method sort of "fails" and indicates this by returning false
. (Note that we're on a level at which it's technically not even an "error"!)
2.Throwing an exception
This is the default option.
Question is now, should you go for a checked exception (which you need a throws
declaration or try
/catch
clauses) or an unchecked exception (an exception that extends RuntimeException
). There are a few rules of thumbs here.
From Java Practices -> Checked versus unchecked exceptions:
Unchecked exceptions: Represent defects in the program (bugs) - often invalid arguments passed to a non-private method.
Checked exceptions: Represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
Note that IllegalArgumentException
is an unchecked exception, perfectly suitable for throwing when arguments are not as they should be.
If you want to throw a checked exception, you could A) roll your own by extending Exception
, B) use some existing checked exception or C) "chain" a runtime exception in, for instance, an IOException
: throw new IOException(new IllegalArgumentException("reason goes here..."));
3.Validating input prior to function call
Relying on the fact that the client should have sanitized / checked his arguments before the call seems like a bad idea to me.
Your second suggestion ("Throwing an exception") is the best choice. The other two options rely on the invoker either doing something before ("Validating input prior to function call") or after ("Flag error checking") the call to the method. Either way, the extra task is not mandated by the compiler so someone invoking the function isn't forced to call the "extra thing" so problems are not spotted till run-time.
As for "Throwing an Exception" and your suggested 'problem', well the answer is throw appropriate exception types for the code. If the input parameters are invalid, then throw an InvalidArgumentException (since that's the appropriate error). If the exception is for functionality (e.g. cannot open network connection), use another exception type or create your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With