try..catch 之后的命令不起作用

发布于 2024-10-20 02:54:03 字数 280 浏览 3 评论 0原文

我有一个带有 try..catch 的方法。 结构是这样的:

try
{
 commands...

}
catch(...)
{
    ERROR(...);
}
if(m_pDoc->m_bS = FALSE ) // Check here if AutoLogout event occurred.
    StartCollect();
}

程序不会进入catch部分,但后面也不会进入if语句。 可能是什么问题?为什么程序不执行if语句?

谢谢

I have a method with try..catch.
the structure is like this:

try
{
 commands...

}
catch(...)
{
    ERROR(...);
}
if(m_pDoc->m_bS = FALSE ) // Check here if AutoLogout event occurred.
    StartCollect();
}

The program doesn't go into the catch section, but it also doesn't go into the if statement later.
What can be the problem? Why doesn't the program go to the if statement?

Thanks

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

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

发布评论

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

评论(6

夏夜暖风 2024-10-27 02:54:03

您的 if 语句几乎肯定是错误的。您将 FALSE 分配给 bSilenClose,然后检查它 (false) 是否为 true,这将导致 if 的主体永远不会执行。在 C++ 中,相等性测试是 ==。此外,正如 @Martin York 指出的那样,尾随的 ; 将被视为 if 的主体。事实上,下面大括号中的代码应该每次都执行。

if(m_pDoc->m_bSilenClose = FALSE );
                         ^       ^^^^ This should not be there. (Empty statement after if)
                         ^
                         ^ Assigning FALSE (should be == to test)
                           Condition always FALSE (thus never executes empty statement.

Your if statement is almost certainly wrong. You're assigning FALSE to bSilenClose and then checking if it (false) is true, which will cause the body of your if to never execute. In C++ the test for equality is ==. Additionally as @Martin York points out, the trailing ; will be treated as the body of your if. The code below in braces should, in fact, execute every time.

if(m_pDoc->m_bSilenClose = FALSE );
                         ^       ^^^^ This should not be there. (Empty statement after if)
                         ^
                         ^ Assigning FALSE (should be == to test)
                           Condition always FALSE (thus never executes empty statement.
当爱已成负担 2024-10-27 02:54:03

catch 仅在发生异常时才会被调用。至于为什么 if 语句中的内容没有被调用:

  • 你的条件语句是错误的,
  • 你的 catch 也可能抛出异常(?)

编辑:刚刚注意到这是 C++。

catch will only be called if an exception occurs. As to why the stuff in the if statement isn't being called, either:

  • your conditional statement is wrong
  • your catch might be throwing an exception too (?)

Edit: just noticed this is C++.

青春如此纠结 2024-10-27 02:54:03

你抓到了什么?您的错误可能是您尝试捕获的错误之外的另一种类型。此外,Catch 可能会引发异常。

真实的代码和更好的描述也总是有帮助的;)

What do you catch? Your error could be of another type than the error you try to catch. Also, the Catch might be throwing an exception.

Real code and a better description always help too ;)

巨坚强 2024-10-27 02:54:03

逐行浏览调试器中的代码块(使用 F10 键)。您应该看到代码确实到达了 if 语句。

Go through the block of code in the debugger, line by line (use the F10 key). You should see that the code does indeed get to the if statement.

知足的幸福 2024-10-27 02:54:03

你有一个错字

if(m_pDoc->m_bSilenClose = FALSE ); 

应该是:

if(m_pDoc->m_bSilenClose == FALSE ); 

You have a typo

if(m_pDoc->m_bSilenClose = FALSE ); 

should be:

if(m_pDoc->m_bSilenClose == FALSE ); 
夜还是长夜 2024-10-27 02:54:03

这就是为什么我更喜欢

使用if ( FALSE == variable )

在与常量进行比较时

That's why i prefer doing

if ( FALSE == variable )

When comparing to a constant

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