473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

assert vs error handling

Hi,

assert and error handling can be used for similar purposes.

When should one use assert instead of try/catch and in which
cases the error handling is preferable?

I've read somewhere that assert could be used to start
an interactive debugger automatically. How do I realize that
on a Linux machine using gcc?

Thank you

Chris
Jul 23 '05 #1
12 6629
I don't think you want to try this.
If an assertion fails, you call a function void doDebug(),
instead of the usual abort() that is called from assert(...).

Then start the debugger, set a breakpoint to doDebug()
and run the program.
If an assertion fails, the debugger will stop and by moving
one step up you can examine the situation.

Stephan

Jul 23 '05 #2
Christian Christmann wrote:
assert and error handling can be used for similar purposes.
No, quite the contrary!!
When should one use assert instead of try/catch and in which
cases the error handling is preferable?
'assert' is for finding bugs in your code in a 'Debug build' (no
asserts remain in the Release build). Exceptions are for gracefully
handling unexpected situations at runtime. Two entirely different use
cases.
I've read somewhere that assert could be used to start
an interactive debugger automatically. How do I realize that
on a Linux machine using gcc?


Never needed that. Isn't it enough that the program prints the
offending line of code?

Jul 23 '05 #3
Christian Christmann wrote:
assert and error handling can be used for similar purposes.

When should one use assert instead of try/catch and in which
cases the error handling is preferable?
Assertions are for programming errors, and error handling is for bad input.

When in doubt, handle an error.
I've read somewhere that assert could be used to start
an interactive debugger automatically. How do I realize that
on a Linux machine using gcc?


On Win32 x86 it's __asm { int 3 };

Someone else will know for Linux. That's the joy of posting an off-topic
question!

Now start writing unit tests, and put all your assertions in there.
Including assertions that bad inputs invoke error handlers.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #4
Phlip wrote:
Assertions are for programming errors, and error handling is for bad input.


I think assertions is often used to check for bad input (preconditions)
as well as for postconditions (design by contract).
Jul 23 '05 #5
Christian Christmann wrote:
Hi,

assert and error handling can be used for similar purposes.

When should one use assert instead of try/catch and in which
cases the error handling is preferable?

I've read somewhere that assert could be used to start
an interactive debugger automatically. How do I realize that
on a Linux machine using gcc?

Thank you

Chris


Some programmers do think that assertions and error handling are
roughly synonymous. But they could hardly be more mistaken. Asserted
conditions and error handling are in fact two unrelated programming
practices that should always be carefully distinguished from one
another. Much of the difficulty in communicating the distinction, I
believe, derives from the fact that the word "error" figures
prominently in the descriptions of each of them. But the types of
errors referred to in either case, are completely different.

Assert expressions are used to find programming errors: either errors
in the program's logic itself or in errors in its corresponding
implementation. An assert condition verifies that the program remains
in a defined state. A "defined state" is basically one that agrees with
the program's assumptions. Note that a "defined state" for a program
need not be an "ideal state" or even "a usual state", or even a "useful
state" but more on that important point later.

To understand how assertions fit into a program, consider a routine in
a C++ program that is about to dereference a pointer. Now should the
routine test whether the pointer is NULL before the dereferencing, or
should it assert that the pointer is not NULL and then go ahead and
dereference it regardless?

I imagine that most developers would want to do both, add the assert,
but also check the pointer for a NULL value, in order not to crash
should the asserted condition fail. On the surface, performing both the
test and the check may seem the wisest decision. But such a decision
shows that information is actually lacking. The programmer does not
know whether a NULL pointer in this situation means that the program is
in undefined state or not. But the program is in one state or the
other. If the pointer is not NULL in every one of the program's defined
states than the assert's NULL check is sufficient. Whatever the program
does afterwards is not covered by the program's design. The best that
can happen is the the program crashes right away. On the other hand, if
the pointer being NULL does not place the application in an undefined
state, then not to check it for a NULL value would be a programming
error before dereferencing it. By the same token, the program's remains
in a defined state regardless of a NULL-valued pointer, so an assert on
the pointer's value would not an assertable condition.

Unlike its asserted conditions, a program's error handling refers not
to errors in the program, but to inputs the program obtains from its
environment. These are often "errors" on someone's part, such as a user
attempting to login to an account without typing in a password. And
even though the error may prevent a successful completion of program's
task, there is no program failure. The program fails to login the user
without a password due to an external error - an error on the user's
part. If the circumstances were different, and the user typed in the
correct password and the program failed to recognize it; then although
the outcome would still be the same, the failure would now belong to
the program.

The purpose of error handling is two fold. The first is to communicate
to the user (or some other client) that an error in program's input has
been detected and what it means. The second aim is to restore the
application after the error is detected, to a well-defined state. Note
that the program itself is not in error in this situation. Granted, the
program may be in a non-ideal state, or even a state in which can do
nothing useful, but there is no programming errorl. On the contrary,
since the error recovery state is one anticipated by the program's
design, it iss one that the program can handle.

To communicate runtime errors, developers will often use asserted
conditions. Unfortunately, this practice further blurs the distinction
between the program's defined and undefined states and between external
and internal errors. For example, testing whether a network server is
reachable is probably not a reasonable asserted condition for a network
application. Unless it is OK to expect the program to crash whenever a
server becomes unreachable, the program should be able to handle this
situation. On the other hand, it is important to convey to a tester
that the reason that the network server cannot be reached is due to an
external condition, and not due to a programming error.

To report external error conditions, I would recommend adopting an
assert-like mechanism, but one that carefully distinguishes these type
of reports from true assertion failures - which are, and should always
be, the detection of a programming error. The rule of thumb should be:
if a program does not crash after an assertion failure then the bug is
either a bogus asserted condition, or a failure in the program's logic
that prevents the program from crashing when it should.

Greg

Jul 23 '05 #6
Greg wrote:
To understand how assertions fit into a program, consider a routine in
a C++ program that is about to dereference a pointer. Now should the
routine test whether the pointer is NULL before the dereferencing, or
should it assert that the pointer is not NULL and then go ahead and
dereference it regardless?

I imagine that most developers would want to do both, add the assert,
but also check the pointer for a NULL value, in order not to crash
should the asserted condition fail. On the surface, performing both the
test and the check may seem the wisest decision. But such a decision
shows that information is actually lacking. The programmer does not
know whether a NULL pointer in this situation means that the program is
in undefined state or not. But the program is in one state or the
other. If the pointer is not NULL in every one of the program's defined
states than the assert's NULL check is sufficient. Whatever the program
does afterwards is not covered by the program's design. The best that
can happen is the the program crashes right away. On the other hand, if
the pointer being NULL does not place the application in an undefined
state, then not to check it for a NULL value would be a programming
error before dereferencing it. By the same token, the program's remains
in a defined state regardless of a NULL-valued pointer, so an assert on
the pointer's value would not an assertable condition.


Hence the value of "unit" tests. If you can write a test case that
legitimately forces a NULL (such as by deleting the program's favorite
file), then you can write an 'if' statement to recover.

If you can't force the situation, add an assertion and keep going.

Note the test case may force a bad situation that client code cannot force.
Hence tests pressure code to grow more robust.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #7
akarl wrote:
Phlip wrote:
Assertions are for programming errors, and error handling is for bad

input.
I think assertions is often used to check for bad input (preconditions)
as well as for postconditions (design by contract).


DbC is magic, and C++ code should not flatter it by imitation.

Without the magic of a fully DbC aware compiler and language definition, an
assertion for a precondition devolves into a feeble attempt to test the
function that called the assertive function.

So, it looks like we are back to automated test cases. They test the calling
function directly, and with a range of data.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand

Jul 23 '05 #8
Greg wrote:
Some programmers do think that assertions and error handling are
roughly synonymous. But they could hardly be more mistaken ...


Please submit your answer to http://www.parashift.com/c++-faq-lite/ .
This really should become a FAQ.

Jul 23 '05 #9


Christian Christmann schreef:
Hi,

assert and error handling can be used for similar purposes.


No. Assertions are essentially comments. i.e. assert( i>5 );
is a better way to write /* after calling foo(), i>5 */
Error handling obviously is not a comment because it affects
functionality.

Regards,
Michiel Salters

Jul 23 '05 #10
msalters wrote:
No. Assertions are essentially comments. i.e. assert( i>5 );
is a better way to write /* after calling foo(), i>5 */


Yay! Hence the "Introduce Assertion Refactor"!

Its canonic form replaces a comment with an assertion...

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #11
Greg wrote:
To understand how assertions fit into a program, consider a routine in
a C++ program that is about to dereference a pointer. Now should the
routine test whether the pointer is NULL before the dereferencing, or
should it assert that the pointer is not NULL and then go ahead and
dereference it regardless?
It depends on the situation. assert(p != NULL) means "p shouldn't be
NULL", whereas if we e.g. are traversing a linked list we will
eventually compare a pointer to NULL, it's expected that the pointer
will become NULL.
Unlike its asserted conditions, a program's error handling refers not
to errors in the program, but to inputs the program obtains from its
environment. These are often "errors" on someone's part, such as a user
attempting to login to an account without typing in a password. And
even though the error may prevent a successful completion of program's
task, there is no program failure. The program fails to login the user
without a password due to an external error - an error on the user's
part. If the circumstances were different, and the user typed in the
correct password and the program failed to recognize it; then although
the outcome would still be the same, the failure would now belong to
the program.


Bad input (invalid values of function parameters) can also occur when a
client is using a library in the wrong way so the distinction is not
nessecarily that clear.

August
Jul 23 '05 #12
akarl wrote:
To understand how assertions fit into a program, consider a routine in
a C++ program that is about to dereference a pointer. Now should the
routine test whether the pointer is NULL before the dereferencing, or
should it assert that the pointer is not NULL and then go ahead and
dereference it regardless?


It depends on the situation. assert(p != NULL) means "p shouldn't be
NULL", whereas if we e.g. are traversing a linked list we will
eventually compare a pointer to NULL, it's expected that the pointer
will become NULL.


In that case you are not about to dereference it.

--
Salu2
Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a...
21
by: Giuseppe | last post by:
is assert() for debug only or not? Is it possible that I have seen the use of assert() in the Borland c++ 32 compiler (so assert is not for debug only)?
27
by: Daniel Vallstrom | last post by:
I'm having problems with inconsistent floating point behavior resulting in e.g. assert( x > 0.0 && putchar('\n') && x == 0.0 ); holding. (Actually, my problem is the dual one where I get...
47
by: Rob Thorpe | last post by:
In general, is it considered bad practice to use asserts in production code? What about writing a macro that does the same as assert but continues to work regardless of the state of NDEBUG? I...
28
by: lovecreatesbeauty | last post by:
Besides printing out for example " a.out: p113.c:8: main: Assertion `0' failed. Aborted " and a switch option NDEBUG, what other benefits does assert() provide in any scope of designing,...
29
by: mailforpr | last post by:
Sometimes, I can't think of any good reason why I should have the program's logic thrown an exception. Except for catching the exception and printing "Uh, oh" to the screen. I also think that in...
5
by: Martin Herbert Dietze | last post by:
Hello, consider this code: | /* warning613.c */ | #include <assert.h> | #include <string.h> | | size_t | myStrLen(char const* s)
7
by: yusufjammy | last post by:
What is the purpose and how to use user defined Assert micros in c++ error handling ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.