Raising an exception vs printing?
exception, printing, python
Solution
Raising an error halts the entire program at that point (unless the exception is caught), whereas printing the message just writes something to `stdout` -- the output might be piped to another tool, or someone might not be running your application from the command line, and the `print` output may never be seen.
For example, what if your code is like:
if size < 0:
print 'size must be non-negative'
else:
print size * 4
and I call your script like:
yours.py number_source.txt | sum_all_lines.sh
If `yours.py` outputs plain text in between numbers, then maybe my `sum_all_lines.sh` will fail because it was expecting all numbers. However, if `yours.py` quits due to an exception, then `sum_all_lines.sh` will not finish, and it will be clear to me why the sum failed.
Of course, that's just one example, and your particular case may be completely different.
Problem
Whats the difference between raising an exception and simply printing an error. For example, whats the benefit of using the following: ``` if size < 0: raise ValueError('number must be non-negative') ``` instead of simply: ``` if size < 0: print 'number must be non-negative' ``` I'm a newbie, please take it easy on me. :)