Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

General question on exception handling

 
Eamon Brown
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50248
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya - good idea re the Exception superclass. Thanks Campbell. Gives me some flexibility...
 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eamon Brown
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50248
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic