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. 🙂
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 theprintoutput may never be seen.For example, what if your code is like:
and I call your script like:
yours.py number_source.txt | sum_all_lines.sh
If
yours.pyoutputs plain text in between numbers, then maybe mysum_all_lines.shwill fail because it was expecting all numbers. However, ifyours.pyquits due to an exception, thensum_all_lines.shwill 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.