Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

Cannot catch `std::invalid_argument` on clang++ under m1 -fno-rtti

Today I got stuck with a weird behaviour with my c++ project. There is the code which doesn't catch exception from standard library (the same snippet works well on intel mac and under linux machines).

Code snippet:

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

int main() {
  try {
    std::stod("notanumber");
  } catch (const std::invalid_argument&) {
    std::cerr << "Caught std::invalid_argument" << std::endl;
  }
}

Compilation command:

clang++ -fno-rtti main.cpp -o main && ./main

Clang versions:

clang++ --version
Homebrew clang version 15.0.3
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /opt/homebrew/opt/llvm/bin

Also tried:

Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

Output:

libc++abi: terminating with uncaught exception of type std::invalid_argument: stod: no conversion
[1]    65643 abort      ./main

Expected output:

Caught std::invalid_argument

Other info:

Chip: Apple M1
OS: 12.6

Why did you supply -fno-rtti? Your example works fine if you drop that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Yes, I know. But it doesn't if I provide the flag. We don't use rtti on our project to reduce binary size

I guess this is also you:

https://stackoverflow.com/questions/74441543/why-stdinvalid-argument-is-not-caught

What happens if you catch without the const ?

I Got this issue when my project use cmake and make .

But I use Xcode to setup C++. command line project,

and config 'Enable C++ Runtime Types' to NO but keep 'Enable C++ Exception' to YES , the program can catch invalid_argument exception from stoi

Cannot catch &#96;std::invalid_argument&#96; on clang&#43;&#43; under m1 -fno-rtti
 
 
Q