Skip to content

Exceptions constructed from std::string bypass catch handlers on Clang 18 #94292

Open
@alexanderchuranov

Description

@alexanderchuranov

Exceptions constructed from an std::string bypass catch handlers and terminate the program. The same exception constructed from a string literal behaves normally. The following program compiled with Clang 14 catches both exceptions. However, on Clang 18 the "dynamic" version terminates the program with the following message:

libc++abi: terminating due to uncaught exception of type std::runtime_error: dynamic

// clang++ -std=c++20
#include <iostream>
#include <stdexcept>
#include <string>

void literal() {
  throw std::runtime_error("literal");
}

void dynamic() {
  throw std::runtime_error(std::string("dynamic"));
}

int main() {
  for (auto f : {literal, dynamic}) {
    try {
      f();
    } catch (std::runtime_error e) {
      std::cerr << "caught: " << e.what() << std::endl;
    }
  }
}

Working compiler:

  • Apple clang version 14.0.3 (clang-1403.0.22.14.1)
  • Target: arm64-apple-darwin22.5.0
  • Thread model: posix

Compiler that fails:

  • Homebrew clang version 18.1.6
  • Target: arm64-apple-darwin22.5.0
  • Thread model: posix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions