473,405 Members | 2,445 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,405 software developers and data experts.

Can we copy stack trace of Exception in a string?

Hi,

Can a stack trace be generated for the exception that the program catches using except or except IOError etc

eg:
Expand|Select|Wrap|Line Numbers
  1.   try:  
  2.        raise Exception
  3.   except Exception,e:
  4.       print the stack trace that caused this exception 
  5.  
Thanks
Feb 22 '07 #1
8 16504
Was looking around online and found this

import sys, traceback
Expand|Select|Wrap|Line Numbers
  1. def func1()
  2.     try:
  3.         do something that raises exception
  4.     except:
  5.         traceback.print_exc(file=sys.stdout)
  6.  
That would print the stack trace.
Feb 22 '07 #2
bartonc
6,596 Expert 4TB
Was looking around online and found this

import sys, traceback
Expand|Select|Wrap|Line Numbers
  1. def func1()
  2.     try:
  3.         do something that raises exception
  4.     except:
  5.         traceback.print_exc(file=sys.stdout)
  6.  
That would print the stack trace.
That looks like just the thing, but I'll check it out for you.
Feb 22 '07 #3
bartonc
6,596 Expert 4TB
Was looking around online and found this

import sys, traceback
Expand|Select|Wrap|Line Numbers
  1. def func1()
  2.     try:
  3.         do something that raises exception
  4.     except:
  5.         traceback.print_exc(file=sys.stdout)
  6.  
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.
Feb 22 '07 #4
ghostdog74
511 Expert 256MB
in your code:

Expand|Select|Wrap|Line Numbers
  1. ....
  2. except Exception,e:
  3.  
you can just print the value of 'e' and see what it says
Feb 23 '07 #5
bartonc
6,596 Expert 4TB
in your code:

Expand|Select|Wrap|Line Numbers
  1. ....
  2. except Exception,e:
  3.  
you can just print the value of 'e' and see what it says
You are just full of cool tricks. Thanks GD.
Feb 23 '07 #6
bvdet
2,851 Expert Mod 2GB
Following are a couple of functions we use to format the error messages:
Expand|Select|Wrap|Line Numbers
  1. import sys, traceback
  2.  
  3. def formatExceptionInfo(level = 6):
  4.     error_type, error_value, trbk = sys.exc_info()
  5.     tb_list = traceback.format_tb(trbk, level)    
  6.     s = "Error: %s \nDescription: %s \nTraceback:" % (error_type.__name__, error_value)
  7.     for i in tb_list:
  8.         s += "\n" + i
  9.     return s
  10.  
  11. def formatExceptionInfo1():
  12.     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.
Feb 23 '07 #7
ghostdog74
511 Expert 256MB
You are just full of cool tricks. Thanks GD.
no problem. Well actually its documentedhere
Also, OP has it included in his code, so i was wondering why he did not use it.
Feb 23 '07 #8
bartonc
6,596 Expert 4TB
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).
Feb 23 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
7
by: Andy Fish | last post by:
Hi, in my c# code I have something like this: try { ... } catch (Exception ex) { ... throw ex; }
3
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...
4
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:...
2
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;
2
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...
0
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...
1
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...
5
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....
0
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
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
marktang
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,...
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...
0
agi2029
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,...

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.