When to use assert() and when to use try catch? -


in situations use them?

try... catch - exceptional conditions, i.e. conditions aren't caused malformed code, may alter normal control flow external unpredictable events.

assertions catching invalid code, i.e. checking if invariant held in function, checking if internal method called right arguments (for public api might still want exception that), etc.

those basic guidelines, conventions vary situation situation , language language.


when you're in doubt, can ask yourself: is specific safety check supposed still there in release code, after test , finish everything? if answer "yes, it's still neccessary then", want exception. otherwise, want assertion.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -