当我运行应用程序时,捕获到异常如何处理? [英] When i run an application an exception is caught how to handle this?

查看:72
本文介绍了当我运行应用程序时,捕获到异常如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用Quick test Professional运行应用程序时,发生了一个异常,如System.ExecutionEngine异常.

如何处理?

将Windows窗体的对象初始化为form1

When I run an application using Quick test professional an exception is occurred as System.ExecutionEngine exception.

How to handle this?

Initialised an object of a Windows Form as form1

if (form1.ShowDialog(IWin32Window owner) == DialogResult.OK)

在此行发生异常.

如何处理?

Reply

at this line exception occurs.

How to handle this?

Reply

推荐答案

您应将代码包装在try catch块中
You should wrap your code in a try catch block
try
{
     ISchemaEditor ptsd = new ProductionTableSchemaDesigner();
     if (ptsd.ShowDialog(IWin32Window owner) == DialogResult.OK)
     {
     }
}
catch(Exception ex)
{
     //log your exception
}


回答后续问题以回应Ryan的回答:

不是捕获到catch块中的 catched 异常.
这仅表示以下两种情况之一:1)您报告不正确;异常被抛出到其他地方,2)捕获了异常,但由于例如错误的异常处理程序而未能检测到它;只需使用调试器即可.

假设您不会错过捕获到的异常(案例2);但是其他地方抛出异常,该怎么办:

在每个线程的堆栈的最顶部捕获异常.确保您永远不会阻止异常在中间的任何地方传播;如果捕获到任何异常,请始终将其重新引发在同一块中或引发另一个异常(除非以某种方式显示异常的最顶层,否则请不要将其完全隐藏).此规则很少有排除项,通常是万一某些设计不当的第三方库时不得已而为之.

要获取完整的异常信息并找到其来源,请在顶级异常处理程序中进行完整的异常转储:包括异常Stack(最重要)并递归地收集所有内部异常的异常信息(使用Exception.InnerException ).您可以将转储保存在某些文件或System.Diagnostics.EventLog中.顺便说一下,这就是您可能需要在CodeProject问题中使用的信息.

对于System.Windows.Forms.Application,"very top"位于主UI周期中.
方法是:向Application.OnThreadException添加处理程序(在UI中显示异常信息);要触发此事件,请将Application.SetUnhandledExceptionMode分配给UnhandledExceptionMode.CatchException.
注意!默认情况下不会发生!

对于WPF,有一种类似的技术.您需要处理事件Application.DispatcherUnhandledException.


另外或替代地,处理事件System.AppDomain.UnhandledExceptionSystem.AppDomain.FirstChanceException.
感谢Espen Harlinn的创意.

另请参阅:如何我会做出一个当滚动条到达底部时会停止的循环吗? [
Answering follow-up Question in response to the Answer by Ryan:

Exception is not catched caught in catch block.
It simply means one of the two: 1) you report is incorrect; exception is thrown elsewhere, 2) exception is caught but you failed to detect it because wrong exception handler, for example; simply use debugger.

Assuming you would not miss caught exception (case 2); but exception is thrown elsewhere, here what to do:

Catch exception at the very top of the stack of for each thread. Make sure you never block exception from propagation anywhere in the middle; if you catch any exception, always re-throw it in the same block or throw another exception (except the very top where you show exception somehow, never hide it completely). There are very few exclusions from this rule, usually as a last resort in case of some ill-designed 3-rd party libraries.

To get full exception information and locate to source of it, make full exception dump in your top-level exception handler: include exception Stack (most important) and collect exception information on all inner exceptions, recursively (use Exception.InnerException). You can save dump in some file or System.Diagnostics.EventLog. By the way, this is the information you may need to use in your CodeProject Questions.

For System.Windows.Forms.Application, "very top" is in the main UI cycle.
This is how: add a handler to Application.OnThreadException (show exception info in UI); to make this event firing, assign Application.SetUnhandledExceptionMode to UnhandledExceptionMode.CatchException.
Attention! It won''t happen by default!

For WPF, there is a similar technique. You need to handle the event Application.DispatcherUnhandledException.


Additionally or alternatively, handle the events System.AppDomain.UnhandledException and System.AppDomain.FirstChanceException.
Credit to Espen Harlinn for the idea.

See also: How do i make a loop that will stop when a scrollbar reaches the bottom[^]
[END EDIT]



—SA


这篇关于当我运行应用程序时,捕获到异常如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆