What to derive a file I/O exception from?

S

Steven T. Hatton

Does anyone have an opinion as to which of the std::exception derivatives is
most suited from deriving file I/O exceptions from? I know I can use just
about anything for an exception, but I really believe it's best to derive
from std::exception, or from its derivatives.
 
A

Alf P. Steinbach

* Steven T. Hatton:
Does anyone have an opinion as to which of the std::exception derivatives is
most suited from deriving file I/O exceptions from?

std::runtime_error
 
D

Dietmar Kuehl

Alf said:
* Steven T. Hatton:

std::runtime_error

'std::ios_base::failure' could also be a reasonable candidate. This
class does, however, not derive from 'std::runtime_error' but only
from 'std::exception'.
 
A

Aaron W. LaFramboise

'std::ios_base::failure' could also be a reasonable candidate.

I disagree. Presently, class failure is thrown by the library to
indicate some combination of state bits have been set. Code that
catches exceptions of this type will be expecting the exception to
indicate this condition, and almost certainly will be broken by this
change.

Furthermore, there is no particularly compelling reason to derive from
this class, as its common meaning is not applicable to a general I/O
error, and thus deriving from this class adds no additional meaning
over simply deriving from std::exception.

The general guideline is to use one of the specific <stdexcept>
exceptions if they fit, otherwise use std::runtime_error for runtime
exceptional conditions (non-bugs), and std::logic_error for bug
conditions.

Arguably, for the latter class of "logic errors," it is better to
simply use assert() or similar, instead of (or in addition to,
depending) throwing an exception.


Aaron W. LaFramboise
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top