c# - 如何创建一个Windows App(C#),在其中可以在消息框中显示各种对象中的所有异常

标签 c# exception error-handling

我正在尝试在C#中使用MVC。因此,表单 Controller 使用身份验证类实例。我正在处理身份验证类中的无效凭据。但是我无法在消息框中显示该消息,因为它不在表单 Controller 类实例中。请帮忙

最佳答案

您不能简单地在身份验证类中抛出异常,并让您的主表单处理该异常吗?

// main form
try
{
    User _loggedOnUser = Authenticate.GetLoggedOnUser();
}
catch (AuthenticationException ex)
{
    MessageBox.Show(this, ex.Message, "Unable to authenticate user");
}


// Authenticate class
// ... do something
if (something == true)
    throw new AuthenticationException("User account has been disabled");

或者,如果异常不合适,则使用返回值:
// main form
User _loggedOnUser = Authenticate.GetLoggedOnUser();
if (_loggedOnUser == null)
    MessageBox.Show(this, "Unable to authenticate the user");

关于c# - 如何创建一个Windows App(C#),在其中可以在消息框中显示各种对象中的所有异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1412614/

相关文章:

java - 无法使用 JavaFX 使 JavaFX 应用程序在 Gluons "Getting started"页面之后工作

curl - PHP 脚本可以在 Mac 上运行,但不能在 Windows 上运行

c# - 发生异常时显示窗体 C#

c# - 创建 UWP 应用程序时如何使用 C# 更改 image.source?

c# - 防止某些字段被 .NET Azure Web Api 中的 PATCH 覆盖

c# - Exchange Online 管理 Powershell 模块和证书 Oauth

c++ - OpenCV:grabcut.cpp 错误

c# - sqlite 位列在每种情况下都返回 'false' 值

c++ - 在 boost asio 中处理未处理的异常并重新启动处理的正确方法

c++ - AIX 5.3 上的跨共享库问题异常