It's not a secret anymore!*
The moose likes Java in General and the fly likes General question on exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "General question on exception handling" Watch "General question on exception handling" New topic
Author

General question on exception handling

Eamon Brown
Greenhorn

Joined: Aug 11, 2008
Posts: 8
Hi - I have a general question on exception handling, and would like to get epoples thoughts on it...I've read a few articles on the net, and there are lots of different opinions out there.

Here's my scenario:

I have a private native method which returns a status code that can either be "ok" or any one of about 30 different error codes.

Based on the value of the error code, I can take up to four different corrective actions.

I have a public method which calls the native method, and translates the error codes into exceptions.

Now, rather than creating 30 exception classes, what I plan on doing is creating 4 exceptions, one for each different corrective action. These exceptions are thrown from the public method that calls the native method.

Does this sound like a reasonable approach?

My motivation for this approach is to avoid having too many exceptions in the throws clause (about thirty!!!) and also to avoid a generic exception with an error code attribute.

Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Welcome to JavaRanch

Yes, that sounds a good idea. You might find it easier to handle the exceptions if you create a (concrete or abstract) EamonsException ( ) class and make all the other Exceptions subclasses of it. That will give you more flexibility; you can catch them all with catch(EamonsException) or catch them individually. You can also make them checked or unchecked depending on what the superclass of EamonsException is.
Eamon Brown
Greenhorn

Joined: Aug 11, 2008
Posts: 8
Ya - good idea re the Exception superclass. Thanks Campbell. Gives me some flexibility...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

You might want to store the error code in your superclass, because it's still information you may want when debugging:

You can of course leave out a few of the constructors, but I usually include all of them just in case I need them later on.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eamon Brown
Greenhorn

Joined: Aug 11, 2008
Posts: 8
Hi Rob - thanks for the reply...yes I was thinking about this. It would be useful to have this for debug, but am i correct in stating that it is bad practise to use the error code atttribute in the exception for handling the actual error?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
Originally posted by Eamon Brown:
. . . but am i correct in stating that it is bad practise to use the error code atttribute in the exception for handling the actual error?
If you can make use of the error code in handling the error, then it is entirely appropriate to include it in the Exception object, as Rob has shown.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

How are you planning on using the error code? You can use the error code in both very good and very bad ways.

One tip though: put your error codes as constants inside your exception class(es). You can then use them as AemonException.ERROR_FILE_NOT_FOUND instead of 1 (for example).

(Yes, that's a DailyWTF reference )
Eamon Brown
Greenhorn

Joined: Aug 11, 2008
Posts: 8
Well...I guess I could catch the exception, take a look at the error code, and depending on the error code, retry the call a specific number of times (how many times changes depending on the code). I'm just wary of using error codes in Java...i had such bad experiences with them in c/c++! Also, articles like this: http://weblogs.java.net/blog/bakksjo/archive/2005/09/java_exception.html advise against it....I guess it's just a judgment call.
 
GeeCON Prague 2014
 
subject: General question on exception handling