上級 › 例外を捕まえ損ねて terminate する原因と直し方 › パターン4
例外を捕まえ損ねて terminate する原因と直し方
throwで投げた例外をどこのtry-catchでも捕まえられず、プログラム全体が異常終了してしまうバグです。
1 #include <iostream> 2 #include <stdexcept> 3 void deposit(int amount) { 4 if (amount < 0) { 5 throw std::invalid_argument("amount must not be negative"); 6 } 7 std::cout << "deposited" << std::endl; 8 } 9 int main() { 10 deposit(___); ^ 11 return 0; 12 }
terminate called after throwing an instance of 'std::invalid_argument'
what(): amount must not be negative
Aborted
空欄を埋めてコンパイル・実行を通す
投げた例外を捕まえるcatchが無いと、mainまで届いてterminateされます。
次の問題