473,322 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

Lie
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?
Jan 13 '08 #1
3 1116
On Jan 13, 4:14 pm, Lie <Lie.1...@gmail.comwrote:
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)
Jan 13 '08 #2

"Lie" <Li******@gmail.comwrote in message
news:78**********************************@e23g2000 prf.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

Jan 13 '08 #3
Lie
On Jan 14, 1:51*am, "Mark Tolonen" <mark.e.tolo...@mailinator.com>
wrote:
"Lie" <Lie.1...@gmail.comwrote in message

news:78**********************************@e23g2000 prf.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.
Jan 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mike Mella | last post by:
I built a simple form where a user can enter and post news items to his site (with PHP, into MySQL). He doesn't know much HTML. Is there some way he can enter some simple text to declare...
28
by: AK | last post by:
Hi, I recently read an advice here that one should try to use make and version control system even if you're the only one working on the program. Is that a good advice? How many of you do that? ...
10
by: Berthold Hoellmann | last post by:
Hello, When I use ./configure --with-thread --with-fpectl --with-signal-module \ --with-pymalloc --enable-shared --with-cxx=g++ make test on 2.3.3 I get
1
by: Alex Elbert | last post by:
Hi I have built dynamic HTMLTable. Now I want to attach it directly to the Email Body - it is already built, so why not to use a ready table. However, I cannot find the way of getting plain HTML...
1
by: Tom | last post by:
Hi, I built several web applications and all work on local and remote servers. Recently, I built one web application which works on local server. However, the login page can be shown on remote...
30
by: bblais | last post by:
Hello, Let me start by saying that I am coming from a background using Matlab (or Octave), and C++. I am going to outline the basic nuts-and-bolts of how I work in these languages, and ask for...
2
by: many_years_after | last post by:
Hi: some one said we can make a python file to an COM object through py2exe , but I haven't got the method. Could any one who knows tell me how to do? Thanks
2
by: Ken | last post by:
there are one web application,"MainApp", built with ASP.NET 1.1. I made another web application,"SubApp", using ASP.NET 2.0. I have to use "SubApp" inside "MainApp" such as...
2
by: Khookie | last post by:
Hi I'm sure not if this is the right newsgroup since this is (possibly) a question about make, but hopefully someone here might know the best way about this. I've built a program that...
12
by: Pioneer | last post by:
Hi, I would be installing a desktop application on a standalone PC. How do I make sure that owner should not be ableto copy that and/or give it to other folks. In short, how to build measures to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.