在业务层中生成的 UI 上显示异常消息的建议方法是什么?

发布于 2024-08-31 03:22:56 字数 198 浏览 2 评论 0原文

是否存在通过使用业务层抛出的异常在表示层中创建用户友好消息的模式或“最佳实践”?

实际上,在许多情况下,我更喜欢抛出应用程序异常,这迫使我在 UI(aspx.cs 页面)上捕获它们。如果过程很复杂,可能会产生许多不同类型的异常,那么我必须有许多 catch 块来产生特定的错误消息。

您想到了更好的方法吗?类似情况的模式可能吗?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-09-07 03:22:56

第一:我认为最好的做法是仅捕获我目前可以处理的代码中的异常。如果我不能处理就让它提升到更高的水平。

第二:有可能全局捕获异常:

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

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

在这种异常处理方法中,所有尚未处理的异常都会被捕获。在这里您可以通知用户发生了“意外”的事情。

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.

南渊 2024-09-07 03:22:56

您可以使用自定义异常类通过异常向 UI 层返回错误。这些自定义异常可能包含对用户有意义的错误消息,因此您可以像显示任何其他错误消息一样显示该消息。

这样,您在 UI 中只需要一个异常处理程序,而不是针对每种类型的错误使用多个异常处理程序...

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文