C++ – How to catch char * exceptions in C++

cexception

I am trying to catch a char * type exception in main() but the program crashes with the following message: terminate called after throwing an instance of 'char const*'
Here is the code:

#include <iostream>

int main ()
{
    char myarray[10];
    try
    {
        for (int n=0; n<=10; n++)
        {
            if (n>9)
            throw "Out of range";
            myarray[n]='a';
        }
    }
    catch (char * str)
    {
        std::cout << "Exception: " << str << std::endl;
    }
    return 0;
}

Best Answer

Use const:

catch (const char * str)
    {
        std::cout << "Exception: " << str << std::endl;
    }