0

I want to design a python program that handles exceptions in other programs , I also want this to access the stack trace in python . I am new t python development but am willing to learn but I do not have a direction on where to proceed . Could somebody please point me to a direction / resources that I could follow and maybe develop these skills , specifically what I should be learning to achieve my goal.

I want to develop this on python 2.7

Thank you for your responses.

EDIT : by handling exceptions , I just want to know what exception occured . Like in Java with try catch blocks where you can print out the stack trace and see if it is an arithmetic / array out of bounds error

Hi I was also thinking of something on this line something like

  try

 (Execute python program here)  // ie import this program 

 except : 1st exception
 except : 2nd exception
 .
 .
 etc

I know how to read from a file , but an unsure if this is correct for just executing a program written by somebody else?

anu bhaisab
  • 97
  • 1
  • 6
  • 1
    you can't handle exceptions in other programs unless you make something like a debugger but that's extremely complicated. it's not a job for a beginner. all you can do if write a program that reopens a program if it crashes or something like that... – Ionut Hulub Sep 30 '12 at 23:19
  • by handling exceptions , I just want to know what exception occured . Like in Java with try catch blocks where you can print out the stack trace and see if it is an arithmetic / array out of bounds error – anu bhaisab Sep 30 '12 at 23:22
  • I get that. programs don't usually signal that exceptions to the outside world, so you can only catch the exceptions inside the program. what you can catch is the exit code of the program, but that only happens when it crashes/exits. – Ionut Hulub Sep 30 '12 at 23:27
  • Hi I was also thinking of something on this line something like try (Execute python program here) // ie import this program except : 1st exception except : 2nd exception . . etc I know how to read from a file , but an unsure if this is correct for just executing a program written by somebody else? – anu bhaisab Oct 01 '12 at 18:22
  • related: [log syntax errors and uncaught exceptions for a python subprocess and print them to the terminal](http://stackoverflow.com/questions/12508752/log-syntax-errors-and-uncaught-exceptions-for-a-python-subprocess-and-print-them) – jfs Oct 01 '12 at 19:31

2 Answers2

0

Take a look at the traceback module. It formats and prints stack traces. You can use this is a top-level exception handler.

import sys
import traceback

try:
    do_something()
except:
    ex, val, tb = sys.exc_info()
    traceback.print_exception(ex, val, tb)

Python itself essentially does this on any uncaught excepton, then exits.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • Hi I was also thinking of something on this line something like try (Execute python program here) // ie import this program except : 1st exception except : 2nd exception . . etc I know how to read from a file , but an unsure if this is correct for just executing a program written by somebody else? – anu bhaisab Oct 01 '12 at 18:19
  • It sounds like what you really want is to run another interpreter in a subprocess and report the error code back. Maybe the [multiprocessing](http://docs.python.org/library/multiprocessing.html) module is what you really need. – Keith Oct 01 '12 at 21:44
0

I am a little confused about your question; if you are only running other python code this will be automatic. You don't need to read any files, just import the python modules you want to use and call their functions. When these throw exceptions they will simply end up in your code and you can deal with them as you see fit, taking into account best practice regarding exception ahndling of course.

For a quick tutorial on python exceptions look here.

Tim Lamballais
  • 1,056
  • 5
  • 10
  • the point is , when a person wants to use it , they should just be able to import it , ie I do not need to call their function but they just need to import my file .. I hope I am making sense . I am confused on how that can happen though – anu bhaisab Oct 01 '12 at 18:33
  • If you need code to execute on import simply call the desired methods in the root level of your module. – Tim Lamballais Oct 04 '12 at 17:13