使用 F5 时,最终似乎没有在 C# 控制台应用程序中执行

发布于 2024-09-13 20:38:55 字数 207 浏览 2 评论 0原文

int i=0;
try{
    int j = 10/i;
}
catch(IOException e){}
finally{
    Console.WriteLine("In finally");
    Console.ReadLine();
}

在VS2008中按F5时,finally块似乎没有执行。 我在控制台应用程序中使用此代码。

int i=0;
try{
    int j = 10/i;
}
catch(IOException e){}
finally{
    Console.WriteLine("In finally");
    Console.ReadLine();
}

The finally block does not seem to execute when pressing F5 in VS2008.
I am using this code in Console Application.

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

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

发布评论

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

评论(5

无人接听 2024-09-20 20:38:55

当遇到未捕获的异常(在本例中为被零除异常)时,Visual Studio 调试器将停止执行。在调试模式下,Visual Studio 更愿意中断执行并在错误源处显示一个弹出框,而不是让应用程序崩溃。这是为了帮助您找到未捕获的错误并修复它们。如果您分离调试器,则不会发生这种情况。

尝试在不附加调试器的情况下从控制台以发布模式运行它,您将看到消息。

The Visual Studio debugger halts execution when you get an uncaught exception (in this case a divide by zero exception). In debug mode Visual Studio prefers to break execution and give you a popup box at the source of the error rather than letting the application crash. This is to help you find uncaught errors and fix them. This won't happen if you detach the debugger.

Try running it in release mode from the console without the debugger attached and you will see your message.

歌枕肩 2024-09-20 20:38:55

如果您希望它在调试时执行,您可以执行以下两件事:

1)捕获正确的异常:


    int i = 0;
    try
    {
        int j = 10 / i;
    }
    catch(DivideByZeroException e){}
    finally
    {
        Console.WriteLine("In finally");
        Console.ReadLine();
    }

2)告诉 Visual Studio 忽略未处理的异常。转到“调试”-->“异常”,然后可以取消选中“公共语言运行时异常”“用户未处理”选项,也可以展开该节点,然后取消选中各个异常类型。

If you want it to execute while debugging there are two things you can do:

1) Catch the correct exception:


    int i = 0;
    try
    {
        int j = 10 / i;
    }
    catch(DivideByZeroException e){}
    finally
    {
        Console.WriteLine("In finally");
        Console.ReadLine();
    }

2) Tell Visual Studio to ignore unhandled exceptions. Go to Debug-->Exceptions, and then you can uncheck the Common Language Runtime Exceptions "User-unhandled" option, or you can expand that node, and uncheck individual exception types.

半岛未凉 2024-09-20 20:38:55

F5 继续应用程序,直到下一个断点或未处理的异常。

我认为您应该使用 F10 而不是进行步骤调试或打开所有异常(已处理或未处理)的中断。

F5 continues the application till the next breakpoint or unhandled exception.

I think you should use F10 rather for step debugging or turn on breaking for all exceptions (handled or not).

不打扰别人 2024-09-20 20:38:55

不要通过 F5 运行您的应用程序。在调试模式下你不能跳过异常,消息框会一次又一次弹出。

相反,构建它并通过 CMD、Far Manager 等运行

Don't run you application via F5. In Debug mode you can't skip exception, message box will pop-up again and again.

Instead build it and run via CMD, Far Manager, etc

踏雪无痕 2024-09-20 20:38:55

作为最终结论,我们都应该同意,如果存在未处理的异常并且应用程序在调试模式下运行,则最终不会被执行。

As the final conclusion we all should agree, if there is an unhandled exception and the application is running in Debugging mode, finally won't get executed.

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