Exception mainly deal with runtime error: At the same time , these
potentially recoverable error. Such as open an unavailable file re request
more memory than is available, they can be exceptions.
A file write operation failed or file access operation because failed
file change or non-exist.
no enough memory
invalid value, which come from user input, not come from you
error code logic.
System communication software invalid protocol, format, or no
response.
unwinding the stack has cost problems, 1) it make progamme 10% larger
and slowlier. So you can use -fno-exceptions to stop it.
Exception specification is add throw in the end of function, no throw means
that it will throw any exceptions, and throw() means it will not throw any
exceptions. Throw( e1, e1) means that it will throw two kinds of exceptions.
In C++, this feature has been unsupported and only one left is
use throw() to indicates that it will not throw any exceptions. At
the same time, exception specification doesn’t use very well with
template.
A simple exception can be a char string, than use "const char* c" to catch
it. A more complicate example is exception class, and you can define the
exception class, and throw exception_class, than use exception_class & ec
to catch it. You don’t need explicit define exception_class object and throw
this object; throw exception_class(); will call constructor and throw this
unnamed object, it is ok.
You can build exception class inside the C++ standard exception system. It
need to derive you class from exception, and redefine function what(); if you
exception class has very tight relationship with you real class, it can be
declared as nested class.
In you destructor, don’t throw any exception, or catch all the exception in
side of it. Or it will call stop the application. see more effective C++
exception chapter.
Compared with return error code, exception has advatnage:
Can catch deeper called function exceptions. If you want to use
return value, deeper called function is hard to deal with.
Difficult to return value, 1) no return value, such as class
constructor, 2) all return value is normal value, such as atoi().
Make happy path and error-handle path clearly.
You can’t omit exception, Any unhandle exception will can
terminate in the end.