Code Fix

上級terminate called after throwing an instance of '...' の原因と直し方 › パターン2

terminate called after throwing an instance of '...' の原因と直し方

throwで投げた例外をどこのtry-catchでも捕まえられず、プログラム全体が異常終了してしまうバグです。

 1  #include <iostream>
 2  #include <stdexcept>
 3  void setAge(int age) {
 4      if (age < 0) {
 5          throw std::invalid_argument("age must not be negative");
 6      }
 7      std::cout << "age set" << std::endl;
 8  }
 9  int main() {
10      setAge(__);
                ^
11      return 0;
12  }
terminate called after throwing an instance of 'std::invalid_argument' what(): age must not be negative Aborted

空欄を埋めてコンパイル・実行を通す

広告
広告スロット(未設定)

terminate called after throwing an instance of '...' の原因と直し方

C++のランタイムはstd::terminateを呼び出し、プロセスを強制終了させます。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)