errno() and strerror() is a typical C language style. In general, you should
detect errors by checking return values, and use errno only to distinguish
among the various causes of an error, such as "File not found" or
"Permission denied."
It’s only necessary to detect errors with errno when a function does not have
a unique, unambiguous, out-of-band error return (that is, because all of its
possible return values are valid; one example is atoi [sic]). In these
cases (and in these cases only; check the documentation to be sure
whether a function allows this), you can detect errors by setting errno
to 0, calling the function, and then testing errno. (Setting errno
to 0 first is important, as no library function ever does that for
you.)
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
System communication software invalid protocol, format, or no
response.
For different runtime errors, you can take different actions. And It’s depends
on you context of application.
stop (call abort and exit)
caller decide what to do (return a error code or use exception)
You can call abort() and exit() to end you problem anytime. Exit will do
some flush file buffers, but without displaying a message, they are both
declared in <cstdlib> head file.
Pay attention, exception and return a error code has the same philosoph. letcaller decide what to do next. But exception can make the code more
clear(see my c book). and can catch deeper called function exceptions. If you
want to use return value, deeper called function is hard to deal
with.
In deeper called function, throw will return to caller function with catch
block directly. Exception use "unwinding the stack" to track all the
auto object in the calling chain, make all the auto obj destructed
properly.
Just like return value, Exception will skip all the statement below the throw,
In C++, It doesn’t support finally statement sometimes. It will cause
memory leaking. That is why local smart pointer can be used to resolve this
problem.
For this problem, you should use smart pointer to declcare a auto object. If
you use string object, It’s ok. So in previous example, you can use
smart pointer, it will help you to avoid memory leakage problem.
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.
Exception handling should be designed into a program rather than just
added on.
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.
In you ctor, If you use new and new failed and throw a exception, the
destructor will not be called. You can use auto_ptr as member data
and use init list to initialzie it. see more effective C++ exception
chapter.
Uncaught exception will call terminate() function. It will call abort() function
to end the problem. but you can use set_terminate(terminate_handler f )
to replace the default terminate() function.
exception-safe is difficult to assure, You can see effective c++ item
29.
Wrong exception-handling mindsets in c++ FAQ website section 17 is good
topic. You need to read it again if you have time.
Confusing logical error with runtime error. For logical error,you need to rewrite code to fix it. For runtime error, youneed to throw it and catch it to responds. logical error
should use assert or if statement to find it.
Use assert to its fullest. precondition assertion to test the validity of the
arguments passed to a method. and use postcondition assertion to test
the validity of the results produced by the method.