• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exception handling

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My application is communicating with a web client. If there is any exception raised in my application, we have to log it and send it back to the web client which will interpret the error code and error message.
Presently we have errorcodes and messages hardcoded for specific Exception situations. for example if I am not able to make a URLConnection, I will log an errorcode say 2020 and a corresponding message, and send it back to the web client as well.
There is an issue about hardcoding the message for a perticular situation. We are solving it be storing the codes and messages in a property file, so that it is possible to change the message for a perticular Exception condition if necessary without having to recompile.
Patrik Naughton's book recommends against using errorcodes, and JDK which is a fairly complex piece of code doesnt uses error codes, but has subclasses of Exception for specific situations.
My question is if it is really necessary to use error codes in java application to keep track of different error conditions. If we write Subclasses of Exception to represent these error conditions, then does this amounts to hardcoding?I would appreciate answer from someone who has worked on c, because errorcodes are used in c,(there must be a reason for that practice).
 
author
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you on the right track. In our applications, we define subclass exceptions for common occurances (i.e ObjectNotFound, DuplicateException). This way the the upper tiers of the application doesn't have to know if the middle layers are doing IO, sql, or whatever. They just have to deal with ObjectNotFound, or UpdateException. Beyond this, we often use a superclass object for all of you applicaton object that has error handling in it to catch our custom exceptions. So if your module lets something by, the parent will examine it and present the "Sorry, we could not find..." if it catches an ObjectNotFound Exception or "Could not update". As well, make your custom exception encapsulate the original error in case you want to use them for debugging. We also use proprety file for error messages so that the interface folks can alter the wording at anytime without touching the code or recompiling. It also comes in handy for internationalization of the application. I hope this helps.
Sean
 
mohit joshi
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the informative response.
 
reply
    Bookmark Topic Watch Topic
  • New Topic