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.
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.
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?
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.
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).
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 by:autobot
Politics is a circus designed to distract you from what is really going on. So is this tiny ad:
a bit of art, as a gift, that will fit in a stocking