I'm new to c++ and am learning how to handle exceptions. I want the program to throw an exception if I try to add two variables of different types. When I compile, I get the mismatch error and the message that + won't work with the two types, but I want it to throw the exception instead.
#include <iostream>
#include <stdexcept>
#include <typeinfo>
using namespace std;
int main() {
try{
int var1 = 6;
string var2 = "7";
if (typeid(var1).name() == typeid(var2).name()){
cout << var1 + var2;
} else {
throw 99;
}
}catch (int e){
cout << "caught a " << e << endl;
}
}