473,385 Members | 1,325 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,385 software developers and data experts.

How to use Assert micros in c++ error handling ?

What is the purpose and how to use user defined Assert micros in c++ error handling ??
Jan 7 '10 #1
7 3394
weaknessforcats
9,208 Expert Mod 8TB
You should not be using asserts in C++. Asserts are from C and are there due to weaknesses in the C language.

An assert becomes a null when the code is compiled for release. Then when the "assert" fails, the production program crashes on your user. Unless, of course, you hand your user a debug version and it crashes dispaying the assert message. Both of these cases are not acceptable.

In C++ you shoule be using try/catch blocks instead of asserts.
Jan 7 '10 #3
Banfa
9,065 Expert Mod 8TB
It's a little more complex than using try/catch blocks instead of asserts. A try/catch block relies on the called code producing an exception where as a assert tests some boolean expression, they are fundamentally different mechanisms.

You would probably have to change the code in places to return errors to throw exceptions as well as using try/catch blocks.
Jan 7 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
Yes, that's correct.

Do not use the assert in C++.
Jan 8 '10 #5
Thanks but please give some example
Jan 8 '10 #6
whodgson
542 512MB
As weaknessforcats would be fully aware (and from my limited reading) the C++ ISO lists the C++ header <cassert> which defines the assert macro. The few references i have read recommend the liberal application of this macro in the boolean context mention by Banfa above primarily to enforce required preconditions during the program development and testing phases. It is understood that it is the OS which reacts to assert when a false condition is triggered.
Why is it undesirable to use the assert macro in this way if the recommended C++ exception handling measures are properly incorporated?
Jan 9 '10 #7
Banfa
9,065 Expert Mod 8TB
Personally I have yet to see a project (actually that isn't true I have seen 1 in 20 years) where the programmers and managers where willing to to use different code builds for debug and release.

The problem is that in the case of undefined behaviour, such as writing outside array bounds, altering the code can have a huge effect on the actual symptoms of the bug. So in my experience anything that could be asserted is generally just tested with normal code.

I do not believe it is particularly undesirable from a code point of view but from a managing the project and managing bug reports it has never been desirable because you want your debug code doing exactly the same as the release code.

The one place I did see it used was in the software for a 3G modem where they were trying to squeeze every ounce of power out of the processor for transmission bandwidth so removing all those tests was seen as desirable.
Jan 9 '10 #8

Sign in to post your reply or Sign up for a free account.

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...
12
by: Christian Christmann | last post by:
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...
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)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.