Consider Visual C++.
This always correct code emits a completely useless warning:
1 2 3 4 5 |
try { ... } catch (const std::exception& ex) { printf("foo\n"); } |
1 |
test.cpp(3): warning C4101: 'ex' : unreferenced local variable |
This never ever correct code doesn’t emit any warning at all:
1 |
throw new exception("foo"); |
Visual C++ is a totally awesome piece of technology. So why doesn’t it work the way I want it to?
The latter example is heaps of fun, no doubt.
The actually always correct code for the catch clause is
catch (const std::exception&)
Write it this way, and you don’t get an error message. You are signaling your intention to handle an exception with a particular signature, but plan not to actually use the exception.
Your code is correct too. But there’s absolutely no harm in specifying the variable name (“ex”). Especially if you got there either by (a) intentionally deleting code in the exception handler such that the exception variable is no longer used, or (b) using code generated by a template (“code snippet”).
(I find the “unreferenced formal parameter” warnings completely useless as well. “‘argv’ : unreferenced formal parameter”.)