捕获异常与未引用的局部变量警告。

时间:2020-12-30 09:34:59

I've the following code:

我下面的代码:

try {
    // do some stuff
}
catch(const my_exception_type& e) {
    LOG("Exception %s", e.what());
    throw;
}

The problem is that in debug build the LOG is defined as #define LOG(...) real_logger(...), but in release build is defined as #define LOG(...) \\ do nothing.

问题在于,在调试构建中,日志被定义为#define LOG(…)real_logger(…),但是在发布版本中定义为#define LOG(…)\\什么都不做。

Of course when I'm compiling my release code in Visual Studio, I'm getting the warning C4101: 'e' : unreferenced local variable.

当然,当我在Visual Studio中编译我的发布代码时,我得到了警告C4101: 'e':未引用的局部变量。

What is the best practice to handle exception logging without generation any unnecessary warnings?

处理异常日志记录的最佳实践是什么?

P.S
I'm doing nothing with the exception except logging and re-throwing it.

P。除了记录和重新抛出异常外,我什么都不做。

2 个解决方案

#1


1  

You can #ifdef each catch line (very invasive) or add just a line in each catch block:

您可以#ifdef每个catch行(非常有侵略性)或在每个catch块中添加一行:

catch(const my_exception_type& e) {
    UNREFERENCED_PARAMETER(e);
    LOG("Exception %s", e.what());
    throw;
}

And the warning is gone. Or, you can #define MY_EXCEPTION_CATCH(...) to define the e parameter only in debug build.

警告就消失了。或者,您可以定义MY_EXCEPTION_CATCH(…)只在调试构建中定义e参数。

#2


1  

You can mark the object as "used" by casting it to void. It has no influence on the generated machine code, but it will suppress the compiler warning.

您可以将对象标记为“used”,将其转换为void。它对生成的机器代码没有影响,但是它会抑制编译器警告。

try {
    // do some stuff
}
catch(const my_exception_type& e) {
    (void)e;
    LOG("Exception %s", e.what());
    throw;
}

#1


1  

You can #ifdef each catch line (very invasive) or add just a line in each catch block:

您可以#ifdef每个catch行(非常有侵略性)或在每个catch块中添加一行:

catch(const my_exception_type& e) {
    UNREFERENCED_PARAMETER(e);
    LOG("Exception %s", e.what());
    throw;
}

And the warning is gone. Or, you can #define MY_EXCEPTION_CATCH(...) to define the e parameter only in debug build.

警告就消失了。或者,您可以定义MY_EXCEPTION_CATCH(…)只在调试构建中定义e参数。

#2


1  

You can mark the object as "used" by casting it to void. It has no influence on the generated machine code, but it will suppress the compiler warning.

您可以将对象标记为“used”,将其转换为void。它对生成的机器代码没有影响,但是它会抑制编译器警告。

try {
    // do some stuff
}
catch(const my_exception_type& e) {
    (void)e;
    LOG("Exception %s", e.what());
    throw;
}