Hi,
Can a stack trace be generated for the exception that the program catches using except or except IOError etc
eg: -
try:
-
raise Exception
-
except Exception,e:
-
print the stack trace that caused this exception
-
Thanks
8 16504
Was looking around online and found this
import sys, traceback -
def func1()
-
try:
-
do something that raises exception
-
except:
-
traceback.print_exc(file=sys.stdout)
-
That would print the stack trace.
Was looking around online and found this
import sys, traceback -
def func1()
-
try:
-
do something that raises exception
-
except:
-
traceback.print_exc(file=sys.stdout)
-
That would print the stack trace.
That looks like just the thing, but I'll check it out for you.
Was looking around online and found this
import sys, traceback -
def func1()
-
try:
-
do something that raises exception
-
except:
-
traceback.print_exc(file=sys.stdout)
-
That would print the stack trace.
1) You'll learn how to use code tags by reading the "POSTING GUIDELINES" or "REPLY GUIDELINES" panel on the right hand side of the page while you are posting.
2) In section 3.12 of the Python Library Reference (in the python help file) it says:
format_exc( [limit]) This is like print_exc(limit) but returns a string instead of printing to a file. New in version 2.4.
in your code:
you can just print the value of 'e' and see what it says
in your code:
you can just print the value of 'e' and see what it says
You are just full of cool tricks. Thanks GD.
bvdet 2,851
Expert Mod 2GB
Following are a couple of functions we use to format the error messages: - import sys, traceback
-
-
def formatExceptionInfo(level = 6):
-
error_type, error_value, trbk = sys.exc_info()
-
tb_list = traceback.format_tb(trbk, level)
-
s = "Error: %s \nDescription: %s \nTraceback:" % (error_type.__name__, error_value)
-
for i in tb_list:
-
s += "\n" + i
-
return s
-
-
def formatExceptionInfo1():
-
return '%s\n%s' % ('The script was cancelled by the user', ''.join(traceback.format_tb(sys.exc_info()[2])))
Phil Adams contributed to the first function.
You are just full of cool tricks. Thanks GD.
no problem. Well actually its documented here
Also, OP has it included in his code, so i was wondering why he did not use it.
no problem. Well actually its documentedhere
Also, OP has it included in his code, so i was wondering why he did not use it.
Yeah. I saw that and assumed that the OP needed the value in a string (due to the title of the post).
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Naresh Agarwal |
last post by:
Hi
Is there a way to copy the stack trace in a string, which is obtained
via printStackTrace function of Exception object.
Essentially I want to log the stack trace in case of any exception in...
|
by: Andy Fish |
last post by:
Hi,
in my c# code I have something like this:
try {
...
} catch (Exception ex) {
...
throw ex;
}
|
by: Mike Schilling |
last post by:
Instances of SystemOutOfMemoryException do not contain a stack trace. Easy
test to verify this:
class OOM
{
public static void Main() {
try {
Object arr = new Object;
} catch...
|
by: Wayne Wengert |
last post by:
I have an aspx page on which I am trying to copy the contents of a textbox
to the client clipboard when the users clicks a button. The button code is
as follows:...
|
by: Lasse Vågsæther Karlsen |
last post by:
If I got the following code:
try
{
// something that might throw an exception
}
catch (Exception ex)
{
// Log contents of ex here
throw;
|
by: news.microsoft.com |
last post by:
Hi all.
If I wanted to write something so that, when an exception was thrown, and
the stack unwound, the stack trace was captured with the values of the
parameters (instead of just the parameter...
|
by: Patrick F |
last post by:
I keep getting this error message and i don't know whats the problem is and
how to solve it. I have a webproject with 2 components (dll) added to it. One
is called PhotoGallery and one is called...
|
by: Patrick F |
last post by:
I keep getting this error message and i don't know whats the problem is and
how to solve it. I have a webproject with 2 components (dll) added to it. One
is called PhotoGallery and one is called...
|
by: Mr. SweatyFinger |
last post by:
WHY CAN'T THE CLOWN -HOLES WHO WROTE ASP.NET PROVIDE AN ERROR LINE
NUMBER???
HONEST TO SH@THOLE PETE
Server Error in '/New Folder (7)' Application....
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
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,...
|
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: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |