C# 下一行或退出未处理的异常
[更新2:问题已解决,请参阅下面我的评论和我的其他帖子。希望这对其他人有所帮助。] [更新:对我的另一个问题的回答是有缺陷的,这是这个问题的基础。在Visual Studio中,2010和now 2012都可以很好地工作。异常处理程序被调用,在VS断开连接后,我说继续。我决定在VS2012 IDE之外进行测试,这是件好事。操作系统捕获了错误,显示e标准“发生未处理的异常”对话框,提供详细信息以及“继续”和“退出”按钮。选择“继续”,只会继续应用程序,而不会将其捕获到我的uber异常处理程序中。选择“完全”,会使应用程序变白并显示标准的关闭窗口对话框。退出按钮也不会调用我的uber异常处理程序 这样做的目的是调用我的异常处理程序。如果我在VS2012 IDE中工作,我不需要uber异常处理程序。该处理程序的目的是为最终用户和beta测试人员,即除我之外的任何人,也就是不会拥有我的开发站的人 下面的代码在IDE之外不起作用。因此,关闭应用程序和继续这两个按钮没有意义,因为永远不会调用异常处理程序。在IDE下运行并使用代码时(为什么这不是在浪费我的时间?),继续不继续,退出不退出。我只是一遍又一遍地看到相同的异常。是的,我正在尝试发布的可能答案 我真的认为这个话题会很老套,用.NET4.5解决。 ] 我添加了一个未处理的异常处理程序,它独立于我的主窗体,我是在Application.Run(new frmMain())调用之前添加的。该处理程序按预期工作,没有问题 问题: 1.是否可能,如果可能,如何强制关闭/结束违规表单(基本上是关闭应用程序) 我在主窗体上添加了一个按钮,在OnClick事件中,我简单地除以零,以便集中模拟一些不好的东西。我正在使用VS2012调试环境运行。自然地,我首先点击了VS allerted给我的有问题的行,但是在继续执行时,我点击了我未处理的异常处理程序。在该处理程序中,一种选择是结束执行。我执行了“Application.Exit()”,但没有任何效果。我一直回到那个有问题的行C# 下一行或退出未处理的异常,c#,visual-studio-2012,unhandled-exception,C#,Visual Studio 2012,Unhandled Exception,[更新2:问题已解决,请参阅下面我的评论和我的其他帖子。希望这对其他人有所帮助。] [更新:对我的另一个问题的回答是有缺陷的,这是这个问题的基础。在Visual Studio中,2010和now 2012都可以很好地工作。异常处理程序被调用,在VS断开连接后,我说继续。我决定在VS2012 IDE之外进行测试,这是件好事。操作系统捕获了错误,显示e标准“发生未处理的异常”对话框,提供详细信息以及“继续”和“退出”按钮。选择“继续”,只会继续应用程序,而不会将其捕获到我的uber异常处理程序中。选
public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
Exception ex = (Exception)e.Exception;
MessageBox.Show("Whoops! Please contact the developers with the"
+ " following information:\n\n" + e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}
JMK要求提供一些示例代码,但并不确定这需要什么
我弹出一个对话框来响应一个未处理的异常。有4个按钮,其中两个用于发布。第一个按钮允许用户继续,而第二个按钮终止应用程序。至少,这是我的想法
private void cmdContinue_Click(object sender, EventArgs e)
{
// Close this dialog and attempt to resume.
this.DialogResult = DialogResult.OK;
this.Close();
}
private void cmdExitApp_Click(object sender, EventArgs e)
{
// Close this dialog and attempt to resume.
this.DialogResult = DialogResult.OK;
this.Close();
// Exit the application.
Application.Exit();
}
continue块(至少在VS2012 IDE中)只保留在有问题的行上。出口块也是如此
就我的异常而言,异常可以是任何东西。这就是我编写它的原因。我专门的异常只是测试代码,然后按钮就消失了。我正在努力创建一个完美的非破坏性应用程序,但是这个目标是虚幻的,直到那时,这个块。这个机制的另一个目的是m代表测试人员向我报告错误
这是我的超快速且肮脏的未处理异常的代码。我必须要小心,因为简单地除以零会被编译器捕获。顺便说一句,未处理的异常通常不会发生在我编写的特定行上,而是发生在方法内对其他人代码的调用中
private void button1_Click(object sender, EventArgs e)
{
// Attempt to throw and unhandled exception to test out the unhandled exception handler.
int iDivider = 0;
int iResult = 5 / iDivider;
}
这里是另一个代码块,即测试应用程序的启动
public static frmMain FormMain = null;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// Turn on global unhandled exception handling.
cExceptions.Initialize();
// Run the application load tasks manually, as Windows will not call this event until the show.
Program.FormMain = new frmMain();
Application.Run(Program.FormMain);
}
public static frmMain FormMain=null;
///
///应用程序的主要入口点。
///
[状态线程]
静态void Main()
{
//
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//启用全局未处理异常处理。
cExceptions.Initialize();
//手动运行应用程序加载任务,因为在显示之前Windows不会调用此事件。
Program.FormMain=new frmMain();
Application.Run(Program.FormMain);
}
有人要我的异常处理程序,所以在这里。基本上,微软提供了示例
/// <summary>
/// Based on example at http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
/// </summary>
internal static class cExceptions
{
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.ControlAppDomain)]
public static void Initialize()
{
AppDomain currentDomain = AppDomain.CurrentDomain;
currentDomain.UnhandledException += new UnhandledExceptionEventHandler(MyHandler);
}
static void MyHandler(object sender, UnhandledExceptionEventArgs args)
{
Exception ex = (Exception)args.ExceptionObject;
// Log the exception to disk.
// Show the exception information to the user.
using (frmExceptions oException = new frmExceptions())
{
// Set the exeption information.
oException.oEx = ex;
// Show the dialog.
oException.ShowDialog();
}
Console.WriteLine("MyHandler caught : " + ex.Message);
}
}
//
///基于以下示例:http://msdn.microsoft.com/en-us/library/system.appdomain.unhandledexception.aspx
///
内部静态类CEException
{
[SecurityPermission(SecurityAction.Demand,Flags=SecurityPermissionFlag.ControlAppDomain)]
公共静态void Initialize()
{
AppDomain currentDomain=AppDomain.currentDomain;
currentDomain.UnhandledException+=新的UnhandledExceptionEventHandler(MyHandler);
}
静态void MyHandler(对象发送方,未处理的ExceptionEventArgs参数)
{
异常ex=(异常)args.ExceptionObject;
//将异常记录到磁盘。
//向用户显示异常信息。
使用(FRMEExceptions oException=新FRMEExceptions())
{
//设置验证信息。
oException.oEx=ex;
//显示对话框。
oException.ShowDialog();
}
Console.WriteLine(“MyHandler捕获:+ex.Message”);
}
}
提前感谢。在Application.Run之前,在类范围内(即字段)保留对
新frmMain()的引用。在异常处理程序中使用它调用frm.Close()
编辑:二读时,看起来像
Application.ThreadException +=
new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
AppDomain.CurrentDomain.UnhandledException +=
new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
public static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
DialogResult result = DialogResult.Abort;
try
{
Exception ex = (Exception)e.Exception;
MessageBox.Show("Whoops! Please contact the developers with the"
+ " following information:\n\n" + e.Exception.Message + e.Exception.StackTrace,
"Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Stop);
}
finally
{
if (result == DialogResult.Abort)
{
Application.Exit();
}
}
}