[Tutor] Getting the real exception clause
Dave Angel
davea at davea.name
Mon Jun 17 01:41:59 CEST 2013
On 06/16/2013 04:26 PM, Jim Mooney wrote:
> '''I'm using general Exception to print out what the exception is
> until I learn them, but
> it will print out "[Errno 2] No such file or directory" in this case,
> when the real
> exception I'd need to use in an except clause is FileNotFoundError.
> How do I get the
> exception I need to use in the except clause from the more English-like message?
> '''
>
> #Using C:\Python33\python.exe on Win 7 in c:\python33\jimprogs
> try:
> fh = open('nosuchdirectory/text/truthyfalsey.txt')
> for line in fh:
> print(line,end='')
> fh.close()
> except Exception as err:
> print(err)
>
Mark is correct. The best way to print out the exception type and
description is to not use try/except, or at least such a base class as
Exception.
But if you have some other reason to do it your way, then just look at
the type of err.
print( type(err), err)
--
DaveA
More information about the Tutor
mailing list