首页 > 解决方案 > 如何替换多行 cpp 代码?

问题描述

我有一个带有多个 catch 语句的 try-catch。捕获语句大约有 30 行代码。许多函数具有相同的 catch 语句。我想用一行代码(将在某处定义,如宏)替换它们。这对 c++ / Microsoft Visual Studio 是否可行?

try {...}
<INSERT SINGLE LINE OF CODE HERE>

代替:

try {...}
catch (HRESULT hr) {...}
catch (std::exception & e) {...}
catch (LPCWSTR e) {...}
catch (...) {...}

谢谢你。

标签: c++visual-studio

解决方案


函数有什么问题?

void common_handler() { /* handle common */ }

try {...}
catch (HRESULT hr)
{
    common_handler();
    // handle specific
}
catch (std::exception & e)
{
    common_handler();
    // handle specific
}
catch (LPCWSTR e)
{
    common_handler();
    // handle specific
}
catch (...)
{
    common_handler();
    // handle specific
}

推荐阅读