开发者

What is the suggested way to show exception messages on UI which were produced in Business Layer?

开发者 https://www.devze.com 2022-12-29 17:12 出处:网络
Is there a pattern OR \'a best practice\' on creating user\'s friendly messages in the presentation layer by using exceptions w开发者_如何学编程hich were thrown from the Business Layer?

Is there a pattern OR 'a best practice' on creating user's friendly messages in the presentation layer by using exceptions w开发者_如何学编程hich 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


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.


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号