error¶
Note
Most interactive use of opan
will not require detailed
knowledge of the custom errors in this module.
The custom exceptions in this module are all subclassed from
opan.error.OpanError
, which itself is a subclass of
Exception
. In addition to the
typical error message included as part of initializing
an Exception
, the custom error subclasses of
OpanError
also define a typecode and a source attribute
(typically a filename or other data source) to allow more finely-grained
definition of error origins and types. In the below example, attempting
to import the source file for this usage page as an OpenBabel XYZ file
quite sensibly results in an error:
>>> x = opan.xyz.OpanXYZ(path='error.rst')
Traceback (most recent call last):
...
XYZError: (XYZFILE) No valid geometry found: XYZ file: error.rst
The custom exception XYZError
is raised
with typecode XYZFILE
, indicating a problem
with the indicated input file. The external data source causing the
exception is included after the final colon (error.rst
, this file).
If no data source is relevant to a given exception, it is omitted.
The subclasses of opan.const.OpanEnum
are equipped with
membership testing of and iteration over their respective typecodes:
>>> 'XYZFILE' in XYZError
True
>>> [tc for tc in sorted(XYZError)]
['DIHED', 'NONPRL', 'OVERWRITE', 'XYZFILE']
Raising these exceptions follows standard syntax, save for the extra ‘typecode’ and ‘source’ parameters:
>>> raise XYZError(XYZError.OVERWRITE, "Spurious overwrite", "Console")
Traceback (most recent call last):
...
XYZError: (OVERWRITE) Spurious overwrite: Console