[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Exceptions - How do you make it work like built-in exceptions?

Lie Ryan

1/13/2008 2:14:00 PM

A built-in exceptions, when raised, would print traceback that points
out the offending code, like this:

Traceback (most recent call last):
File "F:\dir\code.py", line 43, in <module>
a = 1/0 <<<---
ZeroDivisionError: integer division or modulo by zero

a user-made exception, when raised, would print traceback that points
out the code that raises the exception

Traceback (most recent call last):
File "F:\dir\code.py", line 48, in <module>
raise SomeException('Some Exception Message') <<<---
SomeException: Some Exception Message

which is generally of little use (yeah, it's possible to trace the
code from the line number, but sometimes it might not be that easy,
cause the line number is (again) the line number for the raising code
instead of the offending code)

The sample exception was generated from this code:
####
class SomeException(Exception):
pass

try:
a = 1/0
except:
raise SomeException('Some Exception Message')
####

Is it possible to make the user-made exception points out the
offending code?
6 Answers

Chris

1/13/2008 2:47:00 PM

0

On Jan 13, 4:14 pm, Lie <Lie.1...@gmail.com> wrote:
> A built-in exceptions, when raised, would print traceback that points
> out the offending code, like this:
>
> Traceback (most recent call last):
> File "F:\dir\code.py", line 43, in <module>
> a = 1/0 <<<---
> ZeroDivisionError: integer division or modulo by zero
>
> a user-made exception, when raised, would print traceback that points
> out the code that raises the exception
>
> Traceback (most recent call last):
> File "F:\dir\code.py", line 48, in <module>
> raise SomeException('Some Exception Message') <<<---
> SomeException: Some Exception Message
>
> which is generally of little use (yeah, it's possible to trace the
> code from the line number, but sometimes it might not be that easy,
> cause the line number is (again) the line number for the raising code
> instead of the offending code)
>
> The sample exception was generated from this code:
> ####
> class SomeException(Exception):
> pass
>
> try:
> a = 1/0
> except:
> raise SomeException('Some Exception Message')
> ####
>
> Is it possible to make the user-made exception points out the
> offending code?

from sys import exc_info

try:
a = 1/0
except:
type, value, traceback = exc_info()
raise SomeException(type)

Mark Tolonen

1/13/2008 6:52:00 PM

0


"Lie" <Lie.1296@gmail.com> wrote in message
news:7888e20f-0775-46c4-a6e2-3fc3825c5145@e23g2000prf.googlegroups.com...
>A built-in exceptions, when raised, would print traceback that points
> out the offending code, like this:
>
> Traceback (most recent call last):
> File "F:\dir\code.py", line 43, in <module>
> a = 1/0 <<<---
> ZeroDivisionError: integer division or modulo by zero
>
> a user-made exception, when raised, would print traceback that points
> out the code that raises the exception
>
> Traceback (most recent call last):
> File "F:\dir\code.py", line 48, in <module>
> raise SomeException('Some Exception Message') <<<---
> SomeException: Some Exception Message
>
> which is generally of little use (yeah, it's possible to trace the
> code from the line number, but sometimes it might not be that easy,
> cause the line number is (again) the line number for the raising code
> instead of the offending code)
>
> The sample exception was generated from this code:
> ####
> class SomeException(Exception):
> pass
>
> try:
> a = 1/0
> except:
> raise SomeException('Some Exception Message')
> ####
>
> Is it possible to make the user-made exception points out the
> offending code?

The raise statement *was* the offending (unhandled exception) code. The
ZeroDivisionError was handled by your except clause.

You can override the traceback your exception will use with the
three-expression form of the raise statement (See Section 6.9 "The raise
statement" in the Python Reference Manual) by passing the traceback of the
original exception:

###### CODE #####

import sys

class SomeException(Exception):
pass

try:
a=1/0
except:
org_type,org_value,org_traceback = sys.exc_info()
raise SomeException,'had some problems with this code',org_traceback

###### OUTPUT ######

Traceback (most recent call last):
File "exc.py", line 7, in <module>
a=1/0
SomeException: had some problems with this code


--Mark

Lie Ryan

1/14/2008 6:45:00 PM

0

On Jan 14, 1:51 am, "Mark Tolonen" <mark.e.tolo...@mailinator.com>
wrote:
> "Lie" <Lie.1...@gmail.com> wrote in message
>
> news:7888e20f-0775-46c4-a6e2-3fc3825c5145@e23g2000prf.googlegroups.com...
>
>
>
> >A built-in exceptions, when raised, would print traceback that points
> > out the offending code, like this:
>
> > Traceback (most recent call last):
> >  File "F:\dir\code.py", line 43, in <module>
> >    a = 1/0 <<<---
> > ZeroDivisionError: integer division or modulo by zero
>
> > a user-made exception, when raised, would print traceback that points
> > out the code that raises the exception
>
> > Traceback (most recent call last):
> >  File "F:\dir\code.py", line 48, in <module>
> >    raise SomeException('Some Exception Message') <<<---
> > SomeException: Some Exception Message
>
> > which is generally of little use (yeah, it's possible to trace the
> > code from the line number, but sometimes it might not be that easy,
> > cause the line number is (again) the line number for the raising code
> > instead of the offending code)
>
> > The sample exception was generated from this code:
> > ####
> > class SomeException(Exception):
> >    pass
>
> > try:
> >    a = 1/0
> > except:
> >    raise SomeException('Some Exception Message')
> > ####
>
> > Is it possible to make the user-made exception points out the
> > offending code?
>
> The raise statement *was* the offending (unhandled exception) code.  The
> ZeroDivisionError was handled by your except clause.
>

Well, what you meant by offending code and what I meant by offending
code is different, what I meant by offending code as the code that
makes the exception _need_ to be called (i.e. the a=1/0) and in my
view (in this case), anything inside the except clause is not a real
code, as it doesn't do anything "useful" for the program.

> You can override the traceback your exception will use with the
> three-expression form of the raise statement (See Section 6.9 "The raise
> statement" in the Python Reference Manual) by passing the traceback of the
> original exception:
>
> ###### CODE #####
>
> import sys
>
> class SomeException(Exception):
>     pass
>
> try:
>     a=1/0
> except:
>     org_type,org_value,org_traceback = sys.exc_info()
>     raise SomeException,'had some problems with this code',org_traceback
>
> ###### OUTPUT ######
>
> Traceback (most recent call last):
>   File "exc.py", line 7, in <module>
>     a=1/0
> SomeException: had some problems with this code
>
> --Mark

Thanks.

Joe

3/14/2009 1:26:00 AM

0

On Mar 13, 3:27 pm, andyk...@gmail.com wrote:
> You really showed us!  Be PROUD to be a neanderthal creep.  PROUD!

I try, and yes, I am proud :-)

Are you proud being a fence-hopper hugger?


andyk122

3/14/2009 2:01:00 AM

0

On Mar 13, 9:25 pm, Joe <obri6...@aol.com> wrote:

>
> Are you proud being a fence-hopper hugger?


Yes, I'm absolutely fucking proud.

"...From her beacon-hand
Glows world-wide welcome; her mild eyes command
The air-bridged harbor that twin cities frame.
"Keep, ancient lands, your storied pomp!" cries she
With silent lips. "Give me your tired, your poor,
Your huddled masses yearning to breathe free,
The wretched refuse of your teeming shore.
Send these, the homeless, tempest-tost to me,
I lift my lamp beside the golden door!"

—Emma Lazarus, 1883

Joe

3/14/2009 3:57:00 PM

0

On Mar 13, 10:00 pm, andyk...@gmail.com wrote:
> "...From her beacon-hand
> Glows world-wide welcome; her mild eyes command
> The air-bridged harbor that twin cities frame.
> "Keep, ancient lands, your storied pomp!" cries she
> With silent lips. "Give me your tired, your poor,
> Your huddled masses yearning to breathe free,
> The wretched refuse of your teeming shore.
> Send these, the homeless, tempest-tost to me,
> I lift my lamp beside the golden door!"
>
> —Emma Lazarus, 1883

Nice sentiment and definitely applicable in 1883, but they're NOT
talking about illegal aliens who sneak into the country, steal jobs
from others by lying and using false SS numbers and take benefits
away from those who are entitled to the American dream, those who come
legally. So I'm glad you're proud of your stand. I'm proud of mine
too :-)