views:

218

answers:

2

Hi

I am looking for the best way to do exception handling, for example.. when an error occurs in the business logic layer, is the best way to stop the METHOD using a catch and return an EVENT to the presentation layer?

What should this event contain?

Or should i always BUBBLE up exceptions and handle them in the presentation layer?

Anyone have some good links and required reading on this with regards to the best way of handling exceptions and how to handle them in the client ...

For example if i get a NullException in the BLL than i can catch this.. but whats the best way and return to the presentaiton layer and informing it of the issue..

Event? or another try / Catch in the presentation?

A: 

You can do several things;

  1. Focus on improving the user experience when an unexpected error presents itself.

  2. Always log errors either in eventlog or database.

  3. Implement sufficient infrastructure to not let exceptions happen unless they are system exceptions.

  4. Use throw instread of throw exception

Some links to help you:

  1. http://today.java.net/pub/a/today/2003/12/04/exceptions.html

  2. http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html

  3. http://www.codeproject.com/KB/architecture/exceptionbestpractices.aspx

CodeToGlory
A: 

There are several ways to do it:

1) Throwing exceptions with describing message inside.

2) Firing events

3) Using special interfaces to interact with the user.
For example you can implement something like IUiCallbacks interface and send the object, implementing this interface, to the BLL class or method. Later, method in BLL can call IUiCallbacks.SendMessage() or IUiCallbacks.SendError() to notify presentation. And you can have different classes, such as WinFormsUiCallbacks, WebFormsUiCallbacks and SilentUiCallbacks, implementing this interface.

I usually use 1) and 3)

Example of 3) as requested:

public interface IUiCallbacks
{
  void SendMessage(string message);
  void SendException(string message, Exception ex);
}

public class WinFormsUiCallbacks : IUiCallbacks
{
  public void SendMessage(string message)
  {
    MessageBox.Show(message);
  }

  public void SendException(string message, Exception ex)
  {
    MessageBox.Show(string.Format("Unfortunately, the following errror has occurred:{0}{1}", Environment.NewLine, ex.Message));
  }
}

public class OrderService
{
  private IUiCallbacks _iUiCallbacks;
  ...
  public OrderService() { ... }
  public OrderService(IUiCallbacks iUiCallbacks)
  {
    _iUiCallbacks = iUiCallbacks;
  }
  ...
  public void AddOrder(Order order)
  {
    ...
    if(OrderAlreadyExists(order))
    {
      if(_iUiCallbacks != null)
        _iUiCallbacks.SendMessage("The order can not be added, because it is already accepted.");
      return;
    }
    ...
  }
  ...
}

So it can be used like this:

public partial class OrderForm : Form
{
  ...
  public void btnAddOrderFromExcel_Click(...)
  {
    Order order = LoadOrderFromExcel(...);
    OrderService orderService = new OrderService(new WinFormsUiCallbacks());
    orderService.AddOrder(order);
  }
  ...
}
nightcoder
hi thanks for the reply.. Yes very nice! I would look to hear more info on number3, when you say callback ie. EVENT no? Do you have any example code?
I added the example to my answer.
nightcoder