How to manage a exception in a Aplications

  • Thread starter Thread starter Jarsinio
  • Start date Start date
J

Jarsinio

I have some doubts about how managed exception in a VB.net aplication.

In my aplicattion I have 3 clases:
One clase is the UI ( Windows Forms). This class call a second clase
who perform an action ( for example add a item in the database). The
last class is the one who conect to the DB ( ms access)

How I should manage an exception ?
* What I must do when an exception occurs when the class number 3 wants
to make a DB coneection an faild ? or ( when an exception occurs when
the class number 2 wants to insert a record in e DB an faild ?)

* I need to throw the exception to class 2 and the class 2 to de User
Interfece's class?
* Where I should LOG the exception?
* I need to create my Own Exception?
* The User Interface should have the "intelligence" to know all
posibles exceptions?


Please I need help..

Thanks

Alan Garbo
 
Hi Alan,

Here is one possible scenario/architecture:

1. Create exception classes for each level, like UI Exception, Business
Exception, Database Exception. They can encapsulate calls to a standard
exception handling library, like the Exception Handling Application Block
from the Enterprise Library
(http://msdn.microsoft.com/library/?url=/library/en-us/dnpag2/html/EntLib2.asp).
Or if you already have base classes for each layer you can just add as
methods.

2. At each layer catch the exceptions and log at the layer of occurrence
(since you get there most detail from the original exception). You can use
the Logging Application Block for logging the exception info. Throw an
exception to the upper layer of the base type of the layer (and here you can
transform the message to more user friendly text). For example, if you have
an error at the database layer throw a Database Exception. The next layer
should handle all those application specific exceptions and pass them throw
until the go to the UI. As each layer knows that logging is done at the
layer of occurrence there will be no logging at the upper layer to avoid
duplication.

3. At the UI interface display a user friendly message. The advantage here
is that the UI knows based on the type of the exception where it occurred
and it can provide more specific info to the user.

Regards,

Plamen Ratchev
http://www.SQLStudio.com
 
Back
Top