Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Exceptions - How do you make it work like built-in exceptions?

Reply
Thread Tools

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

 
 
Lie
Guest
Posts: n/a
 
      01-13-2008
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?
 
Reply With Quote
 
 
 
 
Chris
Guest
Posts: n/a
 
      01-13-2008
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)
 
Reply With Quote
 
 
 
 
Mark Tolonen
Guest
Posts: n/a
 
      01-13-2008

"Lie" <> wrote in message
news:7888e20f-0775-46c4-a6e2-...
>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

 
Reply With Quote
 
Lie
Guest
Posts: n/a
 
      01-14-2008
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-...
>
>
>
> >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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
don't like, so don't waste your time thinking and buy them. You know,if you don't like one thing, it means nothing for you.</p> fashion t shirts seller Cisco 0 06-13-2011 02:01 AM
`Job Opportunities · Benefits. What's it like to work in Bangalore?Job Opportunities · Benefits. What's it like to work in Bangalore? \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ >jeni Python 0 08-23-2009 06:32 AM
RE;Kontki if you delete kontiki any program you loaded with it in it 'will not work I have tried it with three programs and none work anymore (if you se it just stop download) 1-Twitch Computer Support 5 04-23-2009 02:45 PM
make pls don't work , i help you make web ,help you do business onthe internet xiaobb03@gmail.com Digital Photography 0 03-13-2008 11:28 AM



Advertisments