• 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

Handling multiple exception codes in a single exception object

 
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We have a scenario where in we have to invoke a web service.
This web service on exception situation may return multiple status codes.
We want to raise a custom exception & populate this custom exception with
with all the status codes & status messages returned by the web service.

From this service layer we have to send this exception object back to
the Struts (controller layer). At the controller layer we populate the
ActionMEssages(1 each for a status code) object which finally gets displayed on a error.jsp file.

What would be the best way to handle this multiple status codes scenario.
Do post your thoughts


Regards
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could create a custom "complex" exception that encapsulates many more basic exceptions. As your service method works it can build a list of basic exceptions and add them all to the one big exception.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can build a single Exception carryng a Collection (a List???) of simple Containers. Your Container is a class made up of only two attributes: ErrorCode e Message where the first one is a custom Enum value (if you are using a Java version graeter-equal to 1.5) and the Message is String (probably a code you can use as a key on a localization property file).

Probably the Paul Sturrock suggestion is more far-seeing and flexible. Maybe mine is easier to handle if all you have to do is to visualize some error messages on the Jsp or to put it in a switch statement. I recall you switch can work well with Enums. Doing the same with different types of Exceptions instances would force you to use a bunch of "instance of" inside a multi-branch if-else construct or to loop through the Exception Collection and to intentionally throw them so that a propr catch block can handle them in an adequate way.

Cheers, Francesco.
 
manish ahuja
Ranch Hand
Posts: 312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

Will the complex exception class as you mentioned have a Collection object for storing basic exceptions. Can you ellaborate a bit on the structure of this complex exception class

Regards
reply
    Bookmark Topic Watch Topic
  • New Topic