Forums Register Login

General question on exception handling

+Pie Number of slices to send: Send
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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Ya - good idea re the Exception superclass. Thanks Campbell. Gives me some flexibility...
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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 )
+Pie Number of slices to send: Send
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.
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
https://gardener-gift.com


reply
reply
This thread has been viewed 1050 times.
Similar Threads
Exception Handling...For Marty Hall..and All
Check and Uncheck exception doubt ?
URLyBird passed
Exception handling for NullPointer and more
Which way to go - A design question.
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 07:38:49.