[Q] Are Exceptions used that much in practice?
greg at cosc.canterbury.ac.nz
Thu Dec 14 02:25:58 CET 2000
Jerome Mrozak wrote:
> An example of compulsion is java.lang.Integer.parseInt(), which throws a
> NumberFormatException you *must* deal with or the program won't
Ah, I see what you mean. No, there's never a case where you
really *have* to catch an exception. You always have the
option of ignoring exceptions; an exception which isn't
caught anywhere will simply cause the program to abort
with a traceback.
Sometimes you will want to catch exceptions, but those cases
are very rare, in my experience. Most of the time you can
write code without thinking about exceptions at all.
> I'm aware of examples of Python such as (faking it here):
> while 1:
> input = inputFileObject.readline()
> catch EOF:
Python doesn't report end-of-file by means of an exception;
it returns an empty string.
It does report I/O errors such as failing to open a file
with an exception, which is one of the few cases that you
will probably want to catch. You don't necessarily have
to put a try-except around every file open, though, and
that's probably not the best thing to do. A single
try-except somewhere high up in your program's logic
to catch any failures and report them to the user is
Hope that helps,
Greg Ewing, Computer Science Dept, University of Canterbury,
Christchurch, New Zealand
To get my email address, please visit my web page:
More information about the Python-list