Re: Exception Misconceptions: Exceptions are for unrecoverable errors.
Vladimir Jovic <vladaspams@gmail.com> writes:
Why?
Read any text about exception safety in C++.
This should have probably been "switch" instead of "if" :
Yes, thank you.
What happens if you can not handle the error in this method?
I define ?handle? to be whatever measures are appropriate
for ?me? (=?the current method?) to take in this case.
By this definition I can always handle an error.
2) How do you handle errors in constructors?
Well, I see and know that C++ nearly forces me to use
exceptions in constructors. However, to avoid this, I could
use the following style whenever I can control a class
(but I do not say that I recommend this for C++):
windowattempt attempt( 640, 480 ); /* "windowattempt" is a class. */
window * window; /* "window" is a class, too. */
if( attempt.succeeded() )
{ window = attempt.getwindow(); }
else
{ switch( attempt.error() )
{ ...
3) Simple example : how would you create a method (or a function) that
reads a value (some random integer) from a file and returns that value?
/* "filereader" is an object */
int const value = filereader.nextint();
if( filereader.error() ) /* handle this */ ...
else /* continue */ ...