Why should I use "const" in my catch block?

c++, exception

Solution

Because you are throwing a string literal, and a string literal is the same as a pointer to constant memory, hence the need for `const`.

Problem

``` try { if (isfull()==1) throw "full stack"; else a[top++] = x; } catch (const char *s) { cout<<s; } ``` Why should we use const in the catch block? If I don't use it, I get this error: ``` terminate called after throwing an instance of 'char const*' Aborted (core dumped) ```

Original source