Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use assert instead of exception throwing?

In one of the online courses I am taking on Udacity regarding code debugging, the professor says it's better to use assert instead of exception throwing in the actual code.

Although through out my programming career I have never seen anyone use it.

Do you know why it is in fact? Do you agree?

PS: the below image is Python code, but I guess the idea applies to all programming languages. Also, this course is only 4 month old so I imagine the method taught is the most modern approach.

enter image description here

like image 543
Adel Boutros Avatar asked Feb 23 '13 22:02

Adel Boutros


People also ask

When would you normally use assertion instead of exception?

The key differences between exceptions and assertions are: Assertions are intended to be used solely as a means of detecting programming errors, aka bugs. By contrast, an exception can indicate other kinds of error or "exceptional" condition; e.g. invalid user input, missing files, heap full and so on.

What is a potential benefit of using assert over an exception?

You use exceptions for exceptional situations. For example an out of memory situation or a network failure. You use assert to ascertain that a cetain precondition is met. For example a pointer is not NULL or an integer is within a certain range.

Why to use assert instead of if?

That an “Assert” is used only for validations, where an “If” clause is used for the logic within our code. We can use an “If” clause to determine whether our automation should follow one path or another, but an “Assert” statement to validate the elements within those paths.

Why are exceptions better than assert statements?

Exceptions versus assertions Use assert statements to test for conditions during development that should never be true if all your code is correct. There's no point in handling such an error by using an exception, because the error indicates that something in the code has to be fixed.


1 Answers

Assert is only for debugging, and allows you to check invariants in a one-liner. Asserts and similar macros are used ubiquitously in testing frameworks. With exceptions, you really need to care what the rest of your library or program is doing. Asserts are as simple as it gets.

It will crash the program without any ambiguity as to what caused it - your assert caused it. It is easier to go there in a debugger. An exception may be caught and will not stop the program or may cause side-effects such as stack-unwinding from a place where it wouldn't normally occur, calling all the destructors, etc., when you don't really care about that since you're debugging.

With exceptions, you need to declare functions to throw, must enable exceptions in languages like C++, etc.

If you're debugging interactively and not just running test cases in batch mode, and as your example is in Python, I think you'd find a function that starts pdb and stops the program right there more helpful.

like image 139
deph Avatar answered Sep 30 '22 19:09

deph