Alex Rivera | Logout

Exception vs Assert?

Asked 2009-01-03T20:43:35.033
59

Possible Duplicate:
design by contract tests by assert or by exception?

Is there a rule of thumb to follow when deciding to use exceptions instead of asserts (or vice versa). Right now I do only throw if it's something I think will happen during runtime on the user side (like a socket or file error). Almost everything else I use asserts.

Also, if I were to throw an assert, what is a nice standard object to throw? If I recall correctly there is std::logic_error, but is that not a good object to throw? What would I throw for a missing file or unexpected input (such as from the command line instead of a frontend app)?

Edit
Report

1 Answer

1

As a general rule, I throw exceptions from:

  1. public functions of a package to catch programming errors.
  2. internal functions to report system errors or pass-through sub-system errors.

where I use asserts only internally to catch implementation mistakes.

answered 2009-01-03T21:37:14.817

Your Answer