断言与错误处理 [英] assert vs error handling

查看:319
本文介绍了断言与错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




断言和错误处理可用于类似目的。


何时应该使用assert而不是try / catch并且其中

的情况下错误处理更可取?


我读过某个断言可以用来启动

自动交互式调试器。如何在使用gcc的Linux机器上实现





Chris

解决方案

我不认为你想尝试这个。

如果一个断言失败,你调用一个函数void doDebug( ),

而不是从assert(...)调用的通常的abort()。


然后启动调试器,设置一个断点来doDebug ()

并运行程序。

如果断言失败,调试器将停止并通过移动

一步你可以检查情况。


Stephan


Christian Christmann写道:

断言和错误处理可用于类似目的。


不,恰恰相反!!

什么时候应该使用assert而不是try / catch,其中
情况下错误处理更可取?


''assert''用于在'Debug build'中查找代码中的错误(没有

断言保留在Release版本中)。例外是优雅地处理运行时的意外情况。两个完全不同的使用

的情况。

我已经读过某个地方,断言可以用来自动启动交互式调试器。如何在使用gcc的Linux机器上实现




从不需要。打印

违规行代码是不够的?


Christian Christmann写道:

断言和错误处理可用于类似目的。

何时应使用assert而不是try / catch,其中错误处理是最好?


断言用于编程错误,错误处理用于输入错误。


如有疑问,请处理错误。

我在某处读过断言可以用来自动启动交互式调试器。如何在使用gcc的Linux机器上实现




在Win32 x86上它是__asm {int 3};


别人会知道Linux。这是发布一个非主题

问题的乐趣!


现在开始编写单元测试,并将所有断言放在那里。 />
包括断言坏输入调用错误处理程序。


-

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


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


这篇关于断言与错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆