There's a compelling example in the C++ Standard, [except.throw]/1:
Example:
throw "Help!";
can be caught by a handler of
const char*
type:try { // ... } catch(const char* p) { // handle character string exceptions here }
When you throw via throw "Error: Cannot divide by zero\n";
, the expression after throw
is a string literal, therefore of type array of n const char (where n is the length of the string + 1). This array type is decayed to a pointer [except.throw]/3, therefore the type of the object thrown is char const*
.
Which types are catched by a handler (catch
) is described in [except.handle]/3, and none of the cases apply here, i.e. the const char*
is not catched by a handler of type char*
.