上級 › 例外を捕まえ損ねて terminate する原因と直し方 › パターン5
例外を捕まえ損ねて terminate する原因と直し方
throwで投げた例外をどこのtry-catchでも捕まえられず、プログラム全体が異常終了してしまうバグです。
1 #include <iostream> 2 #include <stdexcept> 3 void divide(int a, int b) { 4 if (b == 0) { 5 throw std::runtime_error("division by zero"); 6 } 7 std::cout << (a / b) << std::endl; 8 } 9 int main() { 10 divide(10, __); ^ 11 return 0; 12 }
terminate called after throwing an instance of 'std::runtime_error'
what(): division by zero
Aborted
空欄を埋めてコンパイル・実行を通す
0除算を検知して例外を投げる設計でも、捕まえなければプロセスは強制終了します。
次の問題