Word VSTO在运行时吞没了异常而无需调试? [英] Word VSTO swallows exceptions at runtime without debugging?

查看:106
本文介绍了Word VSTO在运行时吞没了异常而无需调试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将此代码放置在ThisDocument_Startup之外的Word文档级VSTO解决方案中的某个位置(创建带单击事件的功能区按钮):

Place this code somewhere in a Word document level VSTO solution outside of ThisDocument_Startup (create a ribbon button, with a click event):

int zero = 0;
int divideByZero = 10 / zero;

无需调试即可启动(Ctrl + F5),结果:吞没了异常,其余代码无声地失败了.

Start without debugging (Ctrl + F5), Result: Exception is swallowed, the rest of the code fails silently.

如果将异常 放置在ThisDocument_Startup中,则会看到该异常,但在其他任何地方都不会出现.微软的VSTO论坛和MSDN文档似乎都应该使用Try ... Catch-对于已知的未知数来说这并不是什么大问题.那未知的未知数又如何呢?

The exception will be visible if placed in ThisDocument_Startup, but it appears nowhere else. Microsoft's VSTO forums and the MSDN documentation seem to feel Try...Catch should be used - which is not a big deal for known unknowns. What about the unknown unknowns?

处理托管代码未处理异常的所有常见方法似乎都无效,大概是因为VSTO将托管代码与Office COM Interops一起使用的原因.

All the common ways of dealing with unhandled exceptions for managed code don't seem to work, presumably because of VSTO using managed code with Office COM Interops:

//These don't work
AppDomain.CurrentDomain.UnhandledException ...
System.Windows.Forms.Application.ThreadException ...

我已经阅读了有关对VSTO代码进行故障排除的帖子,这些帖子似乎总是导致尝试...捕获几乎所有内容!

I've read posts about troubleshooting VSTO code that always seems to lead to placing Try...Catch around practically everything!

是否有更好的方法来处理已知和未知(现在是看不见的和无声的!)故障?

Is there any better way to handle known and unknown (now invisible and silent!) failures?

推荐答案

实际上很容易,在运行Office应用程序之前将环境变量VSTO_SUPPRESSDISPLAYALERTS设置为0,然后Office将显示异常并且在启动过程中不会杀死您的加载项有问题的时候.

Actually it is quite easy, set the environmental variable VSTO_SUPPRESSDISPLAYALERTS to 0 before running the office application, then Office will display exceptions and not kill your add-in during startup when there are issues.

还有一个有用的powershell脚本,它将为您执行此类操作,并且在确定VSTO在 http://archive.msdn.microsoft.com/vstotroubleshooter 通过运行vstotroubleshooter.ps1 setdbg开始,它将为您设置VSTO_SUPPRESSDISPLAYALERTS env变量

There is also a useful powershell script which will do this sort of stuff for you and is handy when figuring out when VSTO not working at http://archive.msdn.microsoft.com/vstotroubleshooter Get started by running vstotroubleshooter.ps1 setdbg which will setup the VSTO_SUPPRESSDISPLAYALERTS env variable for you

这篇关于Word VSTO在运行时吞没了异常而无需调试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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