Exception Messages

MRAB python at mrabarnett.plus.com
Mon Oct 15 12:17:22 EDT 2012


On 2012-10-15 17:00, Wanderer wrote:
> How do you get Exceptions to print messages? I have an exception defined like this
>
> class PvCamError(Exception):
>      def __init__(self, msg):
>          self.msg = msg
>
> But when the error is raised I get this:
>
> Traceback (most recent call last):
>    File "C:\Python27\lib\site-packages\ipython-0.12.1-py2.7.egg\IPython\core\interactiveshell.py", line 2538, in run_code
>      exec code_obj in self.user_global_ns, self.user_ns
>    File "<ipython-input-1-fb48da797583>", line 1, in <module>
>      import S477Test
>    File "U:\workspace\camera\src\S477Test.py", line 13, in <module>
>      camera.getSerialNum()
>    File "U:\workspace\camera\src\S477.py", line 131, in getSerialNum
>      num = self.pl.getParamValue(pvcamConstants.PARAM_HEAD_SER_NUM_ALPHA)
>    File "U:\workspace\camera\src\pvcam.py", line 261, in getParamValue
>      raise PvCamError("Unhandled Type: {0}".format(attype))
> PvCamError
>
> Why wasn't the message printed out?
>
You didn't add a __str__ method:

class PvCamError(Exception):
     def __init__(self, msg):
         self.msg = msg
     def __str__(self):
         return self.msg




More information about the Python-list mailing list