Okay, so I've been following a C++ guide, and I recently got to the section on exception handling. I couldn't get any of my code to work - it all produced some variation on the following error;
terminate called after throwing an instance of 'char const*'
Where "char const*" would be the type of whatever I was throwing.
I ctrl-c, ctrl-v 'ed the example code from the guide, and ran it to see if the error was my fault, or if there was something else going on. It produced the same error (above) as my code did.
Here's the code from the guide;
#include "math.h" // for sqrt() function
#include <iostream>
using namespace std;
// A modular square root function
double MySqrt(double dX)
{
// If the user entered a negative number, this is an error condition
if (dX < 0.0)
throw "Can not take sqrt of negative number"; // throw exception of type char*
return sqrt(dX);
}
int main()
{
cout << "Enter a number: ";
double dX;
cin >> dX;
try // Look for exceptions that occur within try block and route to attached catch block(s)
{
cout << "The sqrt of " << dX << " is " << MySqrt(dX) << endl;
}
catch (char* strException) // catch exceptions of type char*
{
cerr << "Error: " << strException << endl;
}
}