I implemented my exception class
which inherited from std::exception
like the following :
class MyException : public exception
{
public:
MyException(string exc)
{
this->exc = exc;
}
const char * what() const throw ()
{
return exc.c_str();
}
private:
string exc;
};
Now i throw MyException
manually and suppose that an exception occurred in my code
, But i tested it in two different ways in catch
section :
Catch normally
catch (const std::exception exc)
try { throw MyException("This is an exception"); } catch (const std::exception exc) { MessageBoxA(NULL, exc.what(), "", MB_OK); }
Catch with reference
catch (const std::exception &exc)
try { throw MyException("This is an exception"); } catch (const std::exception &exc) { MessageBoxA(NULL, exc.what(), "", MB_OK); }
The first thrown exception string is :
Unknown exception
And the second thrown exception (The one that has &
in catch section) is :
This is an exception
Why can't i get my custom exception string while using catch normally without reference(&
) and why is it OK by reference?