In the following codes:
const int ME_ABORT_EXCEPTION = 1;
class CMyException
{
public:
CMyException(int nErrorCode)
: m_nErrorCode(nErrorCode)
{
}
~CMyException()
{
}
int m_nErrorCode;
};
void CTestExceptionDlg::OnBnClickedButton1()
{
// TODO: Add your control notification handler code here
try
{
throw CMyException(ME_ABORT_EXCEPTION);
}
#pragma warning(disable:4101)
catch(CMyException& e)
{
ASSERT(e.m_nErrorCode == ME_ABORT_EXCEPTION);
}
#pragma warning(default:4101)
}
I try to disable the compiler warning C4101 with #pragma, but it does not work. When compiling Release version, there will still be compiler warning. Why?
The warning message is:
warning C4101: 'e' : unreferenced local variable
catch(CMyException&) {...}
is perfectly valid.#pragma warning
is in a function block, it doesn't have effect until after the function block. I don't know offhand if C4101 is in that range, but it is a possibility. In any event, rather than using a pragma (inherently a compiler-specific hook, that will often not work with other compilers) simply change thecatch
tocatch(CMyException &)
(remove the namee
). Also, preferably, make itconst
.catch([[maybe_unused]]CMyException& e)
since C++17.