Win a copy of OCP Oracle Certified Professional Java SE 11 Programmer I Study Guide: Exam 1Z0-815 this week in the Programmer Certification forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Using Exceptions and ActionErrors together

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I am writing a web app with Struts and I would like to do a better job of handling errors.
Okay so I have a Struts action class that calls a class and runs a method that accesses the database and returns the results into a list.
Lets say the DB is messed up or the SQL fails, my Custom SQL class throws out a SQLExeception, with the details of what went wrong.
OKay, so I go through a first method into the bottom one to run this method, if I want to throw that Exception all the way out to the Servlet, do I need of a Throws of the Exeception on each method?
Finally, if I am getting these SQLExeceptions, do I need to keep throwing, or can I just throw a standard with the message text and catch it it the Action and an add an Error to the Errors colelction for the user, and then Log4j the exeception out for the developers?
 
Marshal
Posts: 14371
239
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our approach has been to log exceptions in the method where they are thrown. This helps developers go directly to the source of the problem. Then we'll either rethrow the exception and have it handled higher up in the call stack or throw a custom exception that is caught in the Action to indicate that something bad has happened. Then we'll create a user-friendly ActionError to be displayed to the user instead of the cryptic (to the user, that is) message in the original exception.
 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This calls for a chaining-exception technique.
http://husted.com/struts/tips/015.html
Basically, each layer in your application show have its own exception.
So let say, your Action calls your Service class, which in turns calls your DAO class to get the results you need. You would have a DAOException, and a ServiceException. All your methods in your DAOs would declare DAOExceptions, and all your Service class methods would contain ServiceExceptions. When exceptions occur at your DAO layer (i.e. SQLException), you would wrap this specific error, like SQLException into your DAOException, with a description possibly, and re-throw it back up the layer (which would be the Service). The Service could then re-wrap the DAOException into a ServiceException, and re-throw it back for the clients to handle.
Hope this helps
 
Paul Duer
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does help Hung, both answers were very well done! I like the exception chain idea. I do have a bit of a problem, we have some tags in our JSP pages that can call methods from the service objects, so there it can be hard to get those exceptions to behave, but I think I can work it out.
I have been wanting an excuse to wrap those methods that can be called from a tag in a JSP, they seem to always be problem.
 
Oh the stink of it! Smell my tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!