私のプログラムは次のとおりです:(Linux上)
// Ex. 2 of Exception Handling
// Here divn() raises the exception but main() will be the exception handler
#include<iostream>
using namespace std;
int divn(int n, int d)
{
if(d == 0)
throw "Division by ZERO not possible ";
return n/d;
}
main()
{
int numer, denom;
cout << "Enter the numerator and denominator : ";
cin >> numer >> denom;
try
{
cout << numer << " / " << denom << " = " << divn(numer,denom) << endl;
}
catch(char *msg)
{
cout << msg;
}
cout << "\nEnd of main() \n ";
}
/* 分母を 0 にすると、例外がスローされ、指定されたエラー メッセージが表示されます。denom に 0 を入力すると得られる出力は次のとおりです。
administrator@ubuntu:~/FYMCA/CPP_class$ g++ prog110.cpp administrator@ubuntu:~/FYMCA/CPP_class$ ./a.out 分子と分母を入力してください: 12 0 'char const*' のインスタンスをスローした後に呼び出されて終了します 中止されました(コアダンプ)
問題を解決するにはどうすればよいですか?