java断言与断点,断言与错误处理

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值