catch
我在我的 WinForms 应用程序中实现了以下全局异常:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
try
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MyMainForm());
}
catch (Exception ex)
{
MessageBox.Show("UNHANDLED EXCEPTION: The program will be terminated. Details follow:\n\n" +
getExceptionInfoWithDebuggerOutput(ex),
"Global Exception",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
然后在代码中,引发了一个异常(就像这个一样——完全是由于我的健忘):
public partial class MyPage : UserControl
{
void func1()
{
SqlConnectionStringBuilder conStr = null;
//... later
conStr.DataSource = strServer; //<<--- Where exception is raised
}
}
现在,如果我正在调试我的项目,我Global Exception
会从全局异常处理程序中看到我的消息框。
但是,如果我没有调试我的项目并将其作为 Ctrl+F5 运行,或者如果我构建了一个 Release 项目,我会得到以下窗口,而不是我上面编写的窗口:
知道如何让我的全局异常处理程序代替处理吗?