Exception vs Assert?

C++ExceptionThrow

C++ Problem Overview


> 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)?

C++ Solutions


Solution 1 - C++

My rule of thumb:

Exceptions are used for run-time error conditions (IO errors, out of memory, can't get a database connection, etc.).

Assertions are used for coding errors (this method doesn't accept nulls, and the developer passed one anyway).

For libraries with public classes, throw exceptions on the public methods (because it makes sense to do so). Assertions are used to catch YOUR mistakes, not theirs.

EDIT: This may not be entirely clear, due to the null value example. My point is that you use assertions (as others have pointed out) for conditions that should NEVER happen, for conditions that should NEVER make it into production code. These conditions absolutely must fail during unit testing or QA testing.

Solution 2 - C++

Assert the stuff that you know cannot happen (i.e. if it happens, it's your fault for being incompetent).

Raise exceptional situations which are not treated by the regular control flow of the program.

Solution 3 - C++

You use exceptions for exceptional situations. For example an out of memory situation or a network failure.

You use assert to ascertain that a cetain precondition is met. For example a pointer is not NULL or an integer is within a certain range.

Solution 4 - C++

I use asserts for things that should never happen, yet do. The sort of thing that when it happens, the developer needs to revisit incorrect assumptions.

I use exceptions for everything else.

In reusable code, I prefer an exception because it gives the caller a choice of handling or not handling the problem. Just try catching and handling an assert!

Solution 5 - C++

Assert is a means to verify that the program is in a possible state. If a function returns -1 when it should only return positive integers, and you have an assert that verifies that, your program should stop because it puts your program in a dangerous state.

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser34537View Question on Stackoverflow
Solution 1 - C++Mike HoferView Answer on Stackoverflow
Solution 2 - C++AfdView Answer on Stackoverflow
Solution 3 - C++Toon KrijtheView Answer on Stackoverflow
Solution 4 - C++Paul BeckinghamView Answer on Stackoverflow
Solution 5 - C++LokiView Answer on Stackoverflow
Solution 6 - C++donparkView Answer on Stackoverflow