上級 › terminate called after throwing an instance of '...' の原因と直し方 › パターン1
terminate called after throwing an instance of '...' の原因と直し方
throwで投げた例外をどこのtry-catchでも捕まえられず、プログラム全体が異常終了してしまうバグです。
1 #include <iostream> 2 #include <stdexcept> 3 void withdraw(int balance, int amount) { 4 if (amount > balance) { 5 throw std::runtime_error("insufficient funds"); 6 } 7 std::cout << "ok" << std::endl; 8 } 9 int main() { 10 withdraw(____, 500); ^ 11 return 0; 12 }
terminate called after throwing an instance of 'std::runtime_error'
what(): insufficient funds
Aborted
空欄を埋めてコンパイル・実行を通す
throwで投げた例外をどのtry-catchでも捕まえないと、C++のランタイムはstd::terminateを呼び出しプロセスを強制終了させます。ここでは残高を十分な値にして例外自体が発生しないようにするのが直し方です。
次の問題