Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

General question on exception handling

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 79532
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ya - good idea re the Exception superclass. Thanks Campbell. Gives me some flexibility...
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79532
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanna see my flashlight? How about this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic