Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Handling Errors in Methods

843810Mar 29 2004 — edited Mar 30 2004
I'd like some advice on error handling in Java. I'm more used to using VBA than Java, and typically handle errors as shown below:-

Public Function InsertRecommendTable(bkmTablePosition As Bookmark) As Boolean

'Begin error handled area.
On Error GoTo HandleErr
    'Set the default return value.
    InsertRecommendTable = False	

'Check style exists.
If Not StyleExists(TABLE_STYLE) Then
    'Raise an error.
    Err.Raise CUSTOM_ERROR, _
      "CreateCurrentRecommend.InsertRecommendTable", _
      ErrMessage(18)
End If

'If this point has been reached, the function has been successful.
InsertRecommendTable = True

'The function always exists here.
ExitHere:
    Exit Function

'Error handling takes place here.
HandleErr:
    ShowErrorMessage Err
    Resume ExitHere

End Function

Things I like about this approach are:-

* It's a simple construct to use.

* There's only one way out of the function, and this helps me stay focused when constructing the function. Because; if I'm struggling to get out of the function with an appropriate return value, it suggests to me that the function is incorrect in some way (possibly ill defined, overly elaborate or poorly implemented).

Things that worry me about this approach are:-

* The value returned by the function typically serves as the answer to the function AND is used to indicate the success (or otherwise) of the function. So, the return value is potentially serving two purposes.

* I'm effectively executing a disguised goSub/goto by raising my own errors.


I'm assuming that the Java equivalent would be something like:-

public class testError {

	public boolean createError(String strIPAddr) {

		boolean bSuccess = false;
	
		try {
			System.out.println(InetAddress.getByName(strIPAddr)
					.getCanonicalHostName());
			//Only reach here if the above succeeds.
			bSuccess = true;
		}

		catch (UnknownHostException e) {
			System.out.println("testError specific error.");
			System.err.println("A " + e.getMessage());
		}
		catch (Exception e) {
			System.out.println("testError general error.");
			System.err.println("B " + e.getMessage());
		}

		finally {
			System.out.println("Finally will always be executed.");
			return bSuccess;
		}
	}
}


What I would like to know, (in your experience), is:-

1) Having one return statement in each method viable?

2) Always dealing with exceptions within the method viable?

3) What am I potentially loosing by testing the return value to determine the success of the method, rather than allowing an exception to be propagated and catching that?

4) I've not shown it in the Java example above. But, if a logical error occurs, and I throw and exception to force an exit from the method in the finaly clause, is that a problem? (I know this consumes more resources than just returning at the point the exception occurs).

5) It better to have one large try clause for the whole method, or several localised ones?

I like a nice consistent way of dealing with errors, and dealing with them immediately and locally is easy for me to understand; and this for me, is linked to having one return statement.

However, please let me know if you think this approach is a mistake.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 27 2004
Added on Mar 29 2004
3 comments
413 views