Код: Выделить всё
Exception A: basic_ios::clear: iostream error
Exception B: Failed to close file : Success
< /code>
Сообщение говорит мне, что была ошибка ... это здорово.
Теперь я хочу знать, почему произошла ошибка.-The file does not exist
-The file is not open
-Or something similar
< /code>
Пример код: (очевидно, код преднамеренно сделан, чтобы бросить исключения ... на всякий случай). < /p>
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
#include
#include
#include
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class A : std::ifstream
{
public:
std::string Exception;
bool Close()
{
std::ios_base::iostate Mask = std::ios::failbit | std::ifstream::badbit;
exceptions(Mask);
try
{
close();
}
catch (std::ifstream::failure &e)//std::ios::exceptions
{
Exception = e.what();
return false;
}
return true;
}
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
class B : std::ifstream
{
public:
std::string Exception;
bool Close()
{
try
{
close();
if(fail())
{
throw std::system_error(errno, std::system_category(), "Failed to close file ");
}
}
catch (std::system_error &e)
{
Exception = e.what();
return false;
}
return true;
}
};
//-----------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------
int main(int argc, char **argv)
{
A a;
if(!a.Close())
{
std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/79585410/how-do-i-get-the-reason-for-an-exception[/url]