Word VSTO 在不调试的情况下在运行时吞下异常? [英] Word VSTO swallows exceptions at runtime without debugging?

查看:23
本文介绍了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 代码进行故障排除的帖子,这些帖子似乎总是导致放置 Try...几乎所有内容!

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 环境变量

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天全站免登陆