Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #31317

Re: Exception Messages

Date 2012-10-15 17:17 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Exception Messages
References <8b5cb95f-424b-45a8-b19b-42f92f531a75@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.2217.1350317845.27098.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 09:00 -0700
  Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 17:17 +0100
    Re: Exception Messages John Gordon <gordon@panix.com> - 2012-10-15 16:22 +0000
      Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 17:34 +0100
        Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:18 -0700
          Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:20 -0700
          Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:20 -0700
          Re: Exception Messages MRAB <python@mrabarnett.plus.com> - 2012-10-15 18:34 +0100
            Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:44 -0700
            Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:44 -0700
        Re: Exception Messages Wanderer <wanderer@dialup4less.com> - 2012-10-15 10:18 -0700
      Re: Exception Messages Dave Angel <d@davea.name> - 2012-10-15 12:42 -0400
      Re: Exception Messages Terry Reedy <tjreedy@udel.edu> - 2012-10-15 15:15 -0400
  Re: Exception Messages Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-16 00:47 +0000

csiph-web