views:

44

answers:

2

Hi,

Is there a pattern OR 'a best practice' on creating user's friendly messages in the presentation layer by using exceptions which were thrown from the Business Layer?

Actually in many cases I prefer to throw Application Exceptions and this is forcing me to catch them on UI (aspx.cs pages). And if the process is complex which may produce many different types of exceptions I have to have many catch blocks to produce specific error messages.

Is there a better way coming to your mind? A pattern maybe for similar cases?

thanks

+2  A: 

First: I think it is best practice only to catch exceptions in code I can handle at this time. If I cannot handle just let it promote to higher level.

Second: There is a possibility to catch exceptions globally:

public static void RegisterExceptionHandler()
{
    Application.ThreadException += new System.Threading.ThreadExceptionEventHandler    (Application_UIThreadException);
    Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);

    AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
}

In this exception handling methods all exceptions that have not been handled are catched. Here you can notify the user that something "unexpected" has happend.

Christoph Ungersböck
A: 

You could use a custom exception class to return errors via exception to the UI layer. These custom exceptions could then contain an error message that will be meaningful to the user, so you can display that just like you would any other error message.

That way you will only need a single exception handler in the UI, instead of many for each type of error...

Justin Ethier