Не обрабатывается исключение
#include <iostream>
#include <exception>
using namespace std;
void func(int a)
{
if (a == 0)
throw "Error: Null argument";
}
int main()
{
int a = 0;
try
{
func(a);
}
catch (exception& err)
{
cout <<"Standart exception: " << err.what() << endl;
}
cout << "The end:";
return 0;
}
Здравствуйте, не обрабатывается исключение
Ответы (1 шт):
Автор решения: Mikhailo
→ Ссылка
Чтобы обработать исключение, надо верно указать его тип:
try
{
func(a);
}
catch(char const * const err)
{
cout <<"Non-Standart exception: " << err << endl;
}
catch (exception const & err)
{
cout <<"Standart exception: " << err.what() << endl;
}
Можно также бросать исключение, производное от exception, например,
void func(int a)
{
if (a == 0)
throw runtime_error("Error: Null argument");
}