Funky
Funky

Reputation: 13608

c# Exception handling in 3 tier win forms app

I have a 3 tier win forms app, it has a UI, BLL and DAL. I'm looking to add an exception handler that throws exceptions from the DAL all the way up to the UI so the user knows what the error is. I have tried using try{} catch{} and throwing it from the DAL, but this only throws it up to the BLL and stops there. is there any way to throw it right back to the UI?

Thanks

EDIT:

CODE:

http://pastebin.com/V75MDWdm

Upvotes: 0

Views: 1349

Answers (2)

MattDavey
MattDavey

Reputation: 9027

Each layer should wrap the exception in a slightly more abstract exception.

For example, take a simple customer logon operation:

  • DAL throws a "RecordNotFound" exception when trying to load a "customer" record.
  • BLL catches it, and wraps it in a "InvalidLogonRequest" exception, saying the customer record was not found.
  • GUI catches that, and displays an error message to the user saying the "customer" does not exist.

Each layer below the UI does not necessarily know the wider context of the operation which is in progress, so it's up to the layer above to provide more contextual information about what went wrong...

Maybe a better example would be a cusomer registration operation:

  • DAL throws a sql "UniqueConstraintException" on the email address column.
    • The DAL perhaps doesn't know that this is a registration process, it only knows it's doing an INSERT into the customer table...
  • BLL catches that, and throws a "EmailAlreadyExists" exception.
  • GUI catches that, and displays an message to the user "the email address is already taken"

Upvotes: 7

Rawrgramming
Rawrgramming

Reputation: 1505

The Code provided seems to use a generic Exception to relate the error. this is probably a bad idea, as if you catch any Exceptions anywhere else, that code could easily be consuming this Exception as well when you dont expect it to.

I would specify some custom exceptions for this specific error, and specifically catch/rethrow those Exceptions. using base class Exception will just lead to confusion between different catches in different places.

Upvotes: 2

Related Questions