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
解决方案
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
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?
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