473,396 Members | 1,771 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,396 software developers and data experts.

Retrieving a stacktrace from an exception object / forwarding an exception

Hi,

I am trying to wrap a function that throws an exeption in such a way
that the stacktrace is logged into a file and the exception is
forwarded after that. For example:

-------------------
def my_func():
raise Exception("hello")

def wrapper():
try:
my_func()
except Exception, e:
f = open("logfile", 'a')
f.write(e.stacktrace())
raise e

wrapper() # should throw the exception with a stacktrace showing
my_func()
-------------------

Any idea if and how this can be done?

-Samuel

Jun 22 '07 #1
3 2276
En Fri, 22 Jun 2007 03:54:12 -0300, Samuel <kn******@gmail.comescribió:
I am trying to wrap a function that throws an exeption in such a way
that the stacktrace is logged into a file and the exception is
forwarded after that. For example:

-------------------
def my_func():
raise Exception("hello")

def wrapper():
try:
my_func()
except Exception, e:
f = open("logfile", 'a')
f.write(e.stacktrace())
raise e

wrapper() # should throw the exception with a stacktrace showing
my_func()
-------------------

Any idea if and how this can be done?
- see the traceback module
<http://docs.python.org/lib/module-traceback.html>
- use a bare raise (not raise e); this reraises the active exception
without changing its context.

--
Gabriel Genellina

Jun 22 '07 #2
alg
Use the traceback module:

def log_exception():
"""This function will log the current exception's traceback
to logfile
"""
import traceback
f = open("logfile", 'a')
traceback.print_exc(32, f)
f.close()

def my_func():
raise Exception

def wrapper():
try:
my_func()
except Exception, e:
log_exception()
raise e
On Jun 21, 11:54 pm, Samuel <knipk...@gmail.comwrote:
Hi,

I am trying to wrap a function that throws an exeption in such a way
that the stacktrace is logged into a file and the exception is
forwarded after that. For example:

-------------------
def my_func():
raise Exception("hello")

def wrapper():
try:
my_func()
except Exception, e:
f = open("logfile", 'a')
f.write(e.stacktrace())
raise e

wrapper() # should throw the exception with a stacktrace showing
my_func()
-------------------

Any idea if and how this can be done?

-Samuel

Jun 22 '07 #3
Thanks, Guys, this works as expected.

-Samuel

Jun 22 '07 #4

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

Similar topics

4
by: Barry Mossman | last post by:
Hi, I am throwing an exception derived from ApplicationException as follows catch (Exception ex) { throw new MyException("message", ex); } My exception's constructor is: public...
7
by: clr | last post by:
I like to stamp trace logs with the name of the executing Class and Method. I can get the Class Name using GetType.Name and I can get a list of every Method in the class using...
3
by: Michel Vanderbeke | last post by:
Hello, While logging the errors in my program, I want to know in which class, function or procedure they occured. Is it possible to know the name of the class, function and / or procedure in...
6
by: Kristijan Marin | last post by:
Hi, Can please anyone tell me why do i get only the first method in which the exception was thrown and not the line that triggered the exception ? I don't know if I set something in VS2005 wrong...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.