Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good "Error Checking" Pattern (Java)?

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.

  1. Flag error checking. If doSomething(x) completes successfully return null. Otherwise, it returns a boolean or an error string. Problem: Side effects.

  2. Throwing an exception. Throw an exception if doSomething(x) encounters an error. Problem: If you are performing error checking for parameters only, throwing an IllegalArgumentExceptionseems inappropriate.

  3. 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.

like image 243
volni Avatar asked Jun 10 '10 13:06

volni


People also ask

What is error checking in Java?

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.

What is the most common error in Java?

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.

What are exceptions in Java?

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.

What is error handling in Object Oriented Programming?

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.


3 Answers

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.

like image 115
Daniel Engmann Avatar answered Oct 02 '22 19:10

Daniel Engmann


  1. 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.

like image 25
aioobe Avatar answered Oct 02 '22 18:10

aioobe


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.

like image 43
Phil Avatar answered Oct 02 '22 19:10

Phil