This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Exception handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Exception handling" Watch "Exception handling" New topic
Author

Exception handling

Luc Feys
Greenhorn

Joined: Nov 21, 2005
Posts: 20
Hello,

I have a few questions regarding the handling of exceptions in my project. I have created a three tier application:
*) database layer
*) application layer
*) gui

Now I would like to wrap/translate the database layer exceptions in specific application layer exceptions, so the gui is never exposed to database layer exceptions (which should for example make migration to a SQL database easier).

But this brings up a few questions:
*) Could I create a global Exception handler class that translates all database layer exceptions into app layer exceptions, or would this be a bad programming practice? This would make it easy to wrap all database
exceptions in the application layer the same way, without duplicating code.
Example:


*) Most of my database layer exceptions are pretty low level and not very interesting for my GUI. Except for 'RecordNotFoundException'. Now I have one general 'DataAccessException', but I feel a distinction might be interessing. Would it be bad practice to wrap the 'suncertify.db.RecordNotFoundException' in a 'suncertify.app.RecordNotFoundException', so I can give the GUI user the same Exception information without actually exposing any database layer exceptions?

Any thoughts on this are welcome.

Thanks a lot.

Luc
Mihai Radulescu
Ranch Hand

Joined: Sep 18, 2003
Posts: 916

Hi Loc

I have the same problem but I prefer to chain the exeptions using the initCause(Throwable cause) from the Throwable. This feature provides enought information about the exception and its causes.
If you use a global Exception handler you must be shore that it provides enought information about the exception(and its possible cause).
An other point is the logging - if you use it. I use the throwing everytime when a method is terminating by throwing an exception.


Regards,
Mihai


SCJP, SCJD, SCWCD, OCPJBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception handling
 
Similar Threads
Throw and System.out.println ?
Exception Propogation between Layers
NX: URLy Bird 1.3.1 Explicit Fatal Exception Handling
some final separated questions
GUI Exceptions - bubbled Business exceptions or new ones?