Why `std::invalid_argument` is not caught with no-rtti in macOS M1 environment?
Asked Answered
O

0

14

Today I met a weird behaviour in catching exceptions in C++, could anyone clarify it to me? Code snippe

#include <iostream>
#include <string>
#include <exception>


int main() {
  try {
    std::stod("notanumber");
  } catch (const std::invalid_argument&) {
    std::cerr << "std::invalid_argument" << std::endl;
  } catch (const std::out_of_range&) {
    std::cerr << "std::out_of_range" << std::endl;
  } catch (const std::exception&) {
    std::cerr << "Caught by ancestor" << std::endl;
  } catch (...) {
    auto ptr = std::current_exception();
    auto type = __cxxabiv1::__cxa_current_exception_type();
    std::cerr << type->name() << std::endl;
    std::cerr << "..." << std::endl;
  }
  return 0;
}

Writes to output

St16invalid_argument
...

Environment details

C++ 14, disabled RTTI
Clang 13.1.6 arm64-apple-darwin-21.6.0
macOS Monterey 12.6

I expect exception to be caught on the very first catch block

Upd. Even simplest catch doesn't work for me on the environment

try {
  std::stod("notanumber");
} catch (const std::invalid_argument&) { // not caught
  std::cerr << "std::invalid_argument" << std::endl;
}
Occasion answered 15/11, 2022 at 6:46 Comment(9)
Problem disappears with RTTI turned on, but it's still a question for meOccasion
Without RTTI one cannot downcast with dynamic cast. It's most likely that the exception routines rely on it when catching exceptions.Impunity
Possibly related: discourse.llvm.org/t/….Spreader
@Impunity The type of a thrown exception in C++ is determined at runtime, and all catch handlers (other than, notionally, maybe the ... catch-all`) are required to discriminate based on type of the exception. For that reason, I have trouble imagining a scheme in which an implementation could work correctly without RTTI.Rafaelof
See #21737701 #3638737Mono
See update, even simplest catch with invalid_argument doesn't work on mac m1, but works well on intel. Looks like a compiler bugOccasion
I'm voting to reopen this. The close reason makes no sense at all. This is perfectly reproducible on Mac and there is no typo that causes it.Exceed
With RTTI disabled the compiler is not compiling C++. Check your compiler documentation to see what language features might work in this non-standard environment.Syndesis
Related Clang issue: #66117.Lipps

© 2022 - 2024 — McMap. All rights reserved.