第一次机会例外

发布于 2024-07-15 22:20:11 字数 539 浏览 5 评论 0原文

我一直在浏览 MSDN 帮助文档来掌握 Visual Basic。 在尝试使用计时器 --one 的示例后,将标签和计时器组件拖到设计器中,并将以下内容添加到组件子例程

Label1.Text = My.Computer.Clock.LocalTime.ToLongTimeString

中调试期间立即窗口的输出如下

类型的第一次机会异常 发生“System.InvalidCastException” 在 Microsoft.VisualBasic.dll 中
第一个 类型的偶然异常 发生“System.InvalidCastException” 在 Microsoft.VisualBasic.dll 中

使用上下文菜单组件的先前 MSDN 示例中也会出现相同的错误。 我应该Try...Catch...Finally这个错误并尝试继续吗? 或者,我正在处理更严重的事情吗?

've been running through the MSDN help documents to get a hang of Visual Basic. After trying out the example using timers --one drags a label and timer component into the designer and adds the following to the components subroutine

Label1.Text = My.Computer.Clock.LocalTime.ToLongTimeString

The output for the immediate window during debug is the following

A first chance exception of type
'System.InvalidCastException' occured
in Microsoft.VisualBasic.dll

A first
chance exception of type
'System.InvalidCastException' occured
in Microsoft.VisualBasic.dll

The same error occurs on a previous MSDN example using a context menu component. Should I Try...Catch...Finally this error and try to move on? Or, am I dealing with something much more serious?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我的痛♀有谁懂 2024-07-22 22:20:12

在 IDE 中尝试转到“工具”>“ 选项> 项目与解决方案> VB 默认值并将 Option Strict 设置为“On” - 这可能有助于在编译项目时而不是运行项目时发现转换问题。

“第一次机会执行”并不一定意味着您的代码有问题。 这可能意味着 IDE 或编译器或任何其他涉及的组件遇到并处理了错误,并且在此过程中调试器收到通知,并将异常报告到即时窗口。 这是关于该主题的优秀帖子:

http://blogs .msdn.com/davidklinems/archive/2005/07/12/438061.aspx

In the IDE try going to Tools > Options > Projects and Solutions > VB Defaults and setting Option Strict to 'On' - this may help catch casting problems when you compile your project rather than when you run it.

A 'first chance execption' does not necessarily mean you have a problem in your code. It could mean the IDE or the compiler or any other involved component encountered and handled an error and in the process the debugger is notified and the exception is being reported to the immediate window. This is an excellent post on the topic:

http://blogs.msdn.com/davidklinems/archive/2005/07/12/438061.aspx

所谓喜欢 2024-07-22 22:20:12

调试和诊断第一次机会异常的快速而简单的解决方案是:

AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;

然后

private void CurrentDomainOnFirstChanceException(object sender, FirstChanceExceptionEventArgs firstChanceExceptionEventArgs)
    {
        if (firstChanceExceptionEventArgs.Exception is NullReferenceException)
        {
            // do your handling and debugging :)
        }
    }

运行时期间的多个第一次机会异常可能会削弱应用程序的性能,因为异常处理的成本很高。 尤其是在网络应用程序中。 您可以添加此处理程序并查看特定的第一次机会异常并尝试避免/纠正它们。

A quick and easy solution for debug and diag of First Chance Exception is :

AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;

and then

private void CurrentDomainOnFirstChanceException(object sender, FirstChanceExceptionEventArgs firstChanceExceptionEventArgs)
    {
        if (firstChanceExceptionEventArgs.Exception is NullReferenceException)
        {
            // do your handling and debugging :)
        }
    }

Multiple First Chance Exception during the runtime can cripple the performance of your application because exception handling is expensive. Especially in web apps. You can add this handler and look at specific first chance exceptions and try to avoid/correct them.

雪化雨蝶 2024-07-22 22:20:11

当您看到有关第一次机会异常的信息时,这只意味着您调用的代码中捕获了异常,但并不一定意味着代码失败。 如果代码运行时不会导致程序崩溃并返回有效值,则没有问题。 当您实现自己的 try/catch 块时,您还将在调试窗口中看到有关首次机会异常的输出。

When you see something about a first chance exception, it only means that an exception was caught within the code you called but does not necessarily mean that the code failed. If the code runs without causing your program to crash and returns a valid value, then do not have a problem. You will also see output in the debug window about first chance exceptions when you implement your own try/catch blocks.

情未る 2024-07-22 22:20:11

在调试菜单-> 异常,您可以使调试器在第一次抛出异常时停止,即使稍后会捕获它; 如果你想知道发生了什么,这是最简单的方法

In the Debug menu -> Exceptions, you can enable the debugger to stop when an Exception is first thrown, even if it would be caught later; if you want to find out what's happening, this is the easiest way to do it

榕城若虚 2024-07-22 22:20:11

在第一次出现异常时检查异常的详细信息。 您应该看到堆栈帧/跟踪属性。 在那里您应该看到错误发生在哪一行。 这应该对你有帮助。

In the first chance exception examine the details of the exception. You should see a stack frame/trace property. In there you should see what line the error occurs on. This should help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文