Prev: Struggling to convert a mysql datetime object to a python stringof a different format
Next: Finding the version # of a module, and py module problem
From: Chris Hare on 5 Aug 2010 18:41 I have a block of test code, where I am trying to raise and catch my own user defined exception class NetActiveError(RuntimeError): def __init__(self,error): self.args = error def a(): try: fh = open("me.txt", "r") except Exception as (errno, errText): print errText try: b() except NetActiveError as (errono, errText): print errno, errText def b(): print "def b" raise NetActiveError,"net already running" a() When I run it though, I get the following error: chare$ python z No such file or directory def b Traceback (most recent call last): File "z", line 20, in <module> a() File "z", line 12, in a except NetActiveError as (errono, errText): ValueError: too many values to unpack What am I doing wrong here?
From: Benjamin Kaplan on 5 Aug 2010 18:49 What makes you think it has to do with user-defined exceptions? >>> try : .... raise Exception("hello") .... except Exception as (errno, errText) : .... print "whatever" .... Traceback (most recent call last): ValueError: need more than 1 values to unpack An Exception is an object, not a tuple of number and text. Raise an instance of the exception, not the class: raise NetActiveError("net already running") And then catch the exception object except NetActiveError as err: print err.args On Thu, Aug 5, 2010 at 3:41 PM, Chris Hare <chare(a)labr.net> wrote: > > I have a block of test code, where I am trying to raise and catch my own user defined exception > > class NetActiveError(RuntimeError): > def __init__(self,error): > self.args = error > > def a(): > try: > fh = open("me.txt", "r") > except Exception as (errno, errText): > print errText > try: > b() > except NetActiveError as (errono, errText): > print errno, errText > > def b(): > print "def b" > raise NetActiveError,"net already running" > > > a() > > > When I run it though, I get the following error: > > chare$ python z > No such file or directory > def b > Traceback (most recent call last): > File "z", line 20, in <module> > a() > File "z", line 12, in a > except NetActiveError as (errono, errText): > ValueError: too many values to unpack > > > What am I doing wrong here? > > > -- > http://mail.python.org/mailman/listinfo/python-list >
From: Chris Hare on 5 Aug 2010 19:21 okay - but why does the response come back like No such file or directory def b ('n', 'e', 't', ' ', 'a', 'l', 'r', 'e', 'a', 'd', 'y', ' ', 'r', 'u', 'n', 'n', 'i', 'n', 'g') On Aug 5, 2010, at 5:49 PM, Benjamin Kaplan wrote: > What makes you think it has to do with user-defined exceptions? > >>>> try : > ... raise Exception("hello") > ... except Exception as (errno, errText) : > ... print "whatever" > ... > Traceback (most recent call last): > ValueError: need more than 1 values to unpack > > An Exception is an object, not a tuple of number and text. > > Raise an instance of the exception, not the class: > > raise NetActiveError("net already running") > > And then catch the exception object > > except NetActiveError as err: > print err.args > > On Thu, Aug 5, 2010 at 3:41 PM, Chris Hare <chare(a)labr.net> wrote: >> >> I have a block of test code, where I am trying to raise and catch my own user defined exception >> >> class NetActiveError(RuntimeError): >> def __init__(self,error): >> self.args = error >> >> def a(): >> try: >> fh = open("me.txt", "r") >> except Exception as (errno, errText): >> print errText >> try: >> b() >> except NetActiveError as (errono, errText): >> print errno, errText >> >> def b(): >> print "def b" >> raise NetActiveError,"net already running" >> >> >> a() >> >> >> When I run it though, I get the following error: >> >> chare$ python z >> No such file or directory >> def b >> Traceback (most recent call last): >> File "z", line 20, in <module> >> a() >> File "z", line 12, in a >> except NetActiveError as (errono, errText): >> ValueError: too many values to unpack >> >> >> What am I doing wrong here? >> >> >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > -- > http://mail.python.org/mailman/listinfo/python-list
From: MRAB on 5 Aug 2010 20:37 Chris Hare wrote: > okay - but why does the response come back like > > No such file or directory > def b > ('n', 'e', 't', ' ', 'a', 'l', 'r', 'e', 'a', 'd', 'y', ' ', 'r', 'u', 'n', 'n', 'i', 'n', 'g') > The class Exception saves its arguments in the 'args' instance attribute, and when it prints the exception it prints those arguments: >>> e = Exception(1, 2, 3) >>> print e.args (1, 2, 3) >>> print e (1, 2, 3) >>> print repr(e) Exception(1, 2, 3) NetActiveError inherits from RuntimeError, and ultimately from Exception. NetActiveError sets the 'args' attribute to its single string argument, and when the exception is printed out it thinks it's the arguments: >>> e.args = "string" >>> print e ('s', 't', 'r', 'i', 'n', 'g') >>> print repr(e) Exception('s', 't', 'r', 'i', 'n', 'g') The correct way to create your own exceptions is to call the superclass's __init__ method: >>> class NetActiveError(RuntimeError): .... def __init__(self, error): .... RuntimeError.__init__(self, error) .... >>> e = NetActiveError("string") >>> print e string >>> print repr(e) NetActiveError('string',) > > On Aug 5, 2010, at 5:49 PM, Benjamin Kaplan wrote: > >> What makes you think it has to do with user-defined exceptions? >> >>>>> try : >> ... raise Exception("hello") >> ... except Exception as (errno, errText) : >> ... print "whatever" >> ... >> Traceback (most recent call last): >> ValueError: need more than 1 values to unpack >> >> An Exception is an object, not a tuple of number and text. >> >> Raise an instance of the exception, not the class: >> >> raise NetActiveError("net already running") >> >> And then catch the exception object >> >> except NetActiveError as err: >> print err.args >> >> On Thu, Aug 5, 2010 at 3:41 PM, Chris Hare <chare(a)labr.net> wrote: >>> I have a block of test code, where I am trying to raise and catch my own user defined exception >>> >>> class NetActiveError(RuntimeError): >>> def __init__(self,error): >>> self.args = error >>> >>> def a(): >>> try: >>> fh = open("me.txt", "r") >>> except Exception as (errno, errText): >>> print errText >>> try: >>> b() >>> except NetActiveError as (errono, errText): >>> print errno, errText >>> >>> def b(): >>> print "def b" >>> raise NetActiveError,"net already running" >>> >>> >>> a() >>> >>> >>> When I run it though, I get the following error: >>> >>> chare$ python z >>> No such file or directory >>> def b >>> Traceback (most recent call last): >>> File "z", line 20, in <module> >>> a() >>> File "z", line 12, in a >>> except NetActiveError as (errono, errText): >>> ValueError: too many values to unpack >>> >>> >>> What am I doing wrong here? >>>
From: Chris Hare on 5 Aug 2010 21:28
On Aug 5, 2010, at 7:37 PM, MRAB wrote: > Chris Hare wrote: >> okay - but why does the response come back like >> No such file or directory >> def b >> ('n', 'e', 't', ' ', 'a', 'l', 'r', 'e', 'a', 'd', 'y', ' ', 'r', 'u', 'n', 'n', 'i', 'n', 'g') > The class Exception saves its arguments in the 'args' instance > attribute, and when it prints the exception it prints those arguments: > > > >>> e = Exception(1, 2, 3) > >>> print e.args > (1, 2, 3) > >>> print e > (1, 2, 3) > >>> print repr(e) > Exception(1, 2, 3) > > > NetActiveError inherits from RuntimeError, and ultimately from > Exception. > > NetActiveError sets the 'args' attribute to its single string argument, > and when the exception is printed out it thinks it's the arguments: > > > >>> e.args = "string" > >>> print e > ('s', 't', 'r', 'i', 'n', 'g') > >>> print repr(e) > Exception('s', 't', 'r', 'i', 'n', 'g') > > > The correct way to create your own exceptions is to call the > superclass's __init__ method: > > > >>> class NetActiveError(RuntimeError): > ... def __init__(self, error): > ... RuntimeError.__init__(self, error) > ... > >>> e = NetActiveError("string") > >>> print e > string > >>> print repr(e) > NetActiveError('string',) > > >> On Aug 5, 2010, at 5:49 PM, Benjamin Kaplan wrote: >>> What makes you think it has to do with user-defined exceptions? >>> >>>>>> try : >>> ... raise Exception("hello") >>> ... except Exception as (errno, errText) : >>> ... print "whatever" >>> ... >>> Traceback (most recent call last): >>> ValueError: need more than 1 values to unpack >>> >>> An Exception is an object, not a tuple of number and text. >>> >>> Raise an instance of the exception, not the class: >>> >>> raise NetActiveError("net already running") >>> >>> And then catch the exception object >>> >>> except NetActiveError as err: >>> print err.args >>> >>> On Thu, Aug 5, 2010 at 3:41 PM, Chris Hare <chare(a)labr.net> wrote: >>>> I have a block of test code, where I am trying to raise and catch my own user defined exception >>>> >>>> class NetActiveError(RuntimeError): >>>> def __init__(self,error): >>>> self.args = error >>>> >>>> def a(): >>>> try: >>>> fh = open("me.txt", "r") >>>> except Exception as (errno, errText): >>>> print errText >>>> try: >>>> b() >>>> except NetActiveError as (errono, errText): >>>> print errno, errText >>>> >>>> def b(): >>>> print "def b" >>>> raise NetActiveError,"net already running" >>>> >>>> >>>> a() >>>> >>>> >>>> When I run it though, I get the following error: >>>> >>>> chare$ python z >>>> No such file or directory >>>> def b >>>> Traceback (most recent call last): >>>> File "z", line 20, in <module> >>>> a() >>>> File "z", line 12, in a >>>> except NetActiveError as (errono, errText): >>>> ValueError: too many values to unpack >>>> >>>> >>>> What am I doing wrong here? >>>> > > -- > http://mail.python.org/mailman/listinfo/python-list okay - thanks for the tutorial -- you explained what I couldn't find in the docs I looked at. I appreciate your help |