I'm working in C++. I have a try...catch block, and the catch block catches runtime_error(error message). I want to do different things based on the different error messages. Here's some sample code:
#include <iostream>
#include <stdexcept>
#include <string>
using namespace std;
int main()
{
string a = "hello";
string b = "there";
string c = "there";
try{
if(a == b){
throw runtime_error("error 1");
}
if(b == c){
throw runtime_error("error 2");
}
}
catch(const runtime_error& error){
cout << "error message: " << error.what();
if(error.what() == "error 1"){
// do thing a
}
else if(error.what() == "error 2"){
// do thing b
}
else{
// do thing c
}
}
}
The type of error.what() is not a string, though, (apparently it's PKc, whatever that means), and so I was looking for a way to do different things based on the error message in error.what(). Help
I tried error.what() == "error 1", which did not return true when error.what() was "error 1" apparently. Not entirely sure why, not sure what's going on here. I expected it to return true, but it doesn't.