-2

The code fails to print True because the comparison fails for some reason. I don't know what it's, but it works if I change e.what() == "Something Bad happened here" to e.what() == std::string("Something Bad happened here")

#include <iostream>
#include <string>
#include <stdexcept>

int main() {
    try
    {

        throw std::runtime_error("Something Bad happened here");

    }
    catch(std::exception const& e)
    {
        if(e.what() == "Something Bad happened here") {
            std::cout << "True" << "\n";
        }
    } 
}
phuclv
  • 37,963
  • 15
  • 156
  • 475
jeffbRTC
  • 1,941
  • 10
  • 29

1 Answers1

4

Because std::exception::what() returns a const char*, and a "string literal" is a const char[N]. Operator == on them does a comparison of two pointers. You must use strcmp() on them

if (strcmp(e.what(), "Something Bad happened here") == 0) // ...

std::string OTOH has an operator== to compare with a const char*


If you have C++14 then you can get a std::string without an explicit constructor with the s suffix

using namespace std::string_literals;
if (e.what() == "Something Bad happened here"s) // ...

But of course a std::string is still needed to be constructed before comparing, which may be a little bit slower than strcmp when SSO doesn't kick in


In C++17 there's std::string_view which needs pretty much zero cost to construct from a string literal, so you can use this

using namespace std::literals::string_view_literals;
if (e.what() == "Something Bad happened here"sv) // ...
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    In C++17: `std::string_view(e.what()) == "foo"` or `e.what() == "foo"sv`. – HolyBlackCat May 19 '21 at 11:03
  • @HolyBlackCat yes I did look at `std::string_view` but I misread it so I thought you can't do `==` on `std::string_view` and `const char*` – phuclv May 19 '21 at 11:07