Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify what function call raise an exception in Python?

i need to identify who raise an exception to handle better str error, is there a way ?

look at my example:

try:
   os.mkdir('/valid_created_dir')
   os.listdir('/invalid_path')
except OSError, msg:

   # here i want i way to identify who raise the exception
   if is_mkdir_who_raise_an_exception:
      do some things

   if is_listdir_who_raise_an_exception:
      do other things ..

how i can handle this, in python ?

like image 229
Roberto Martelloni Avatar asked Mar 04 '10 14:03

Roberto Martelloni


People also ask

How do I identify exceptions in Python?

To get exception information from a bare exception handler, you use the exc_info() function from the sys module. The sys. exc_info() function returns a tuple that consists of three values: type is the type of the exception occurred.

Which exceptions are raised?

An exception is a runtime error or warning condition, which can be predefined or user-defined. Predefined exceptions are raised implicitly (automatically) by the runtime system. User-defined exceptions must be raised explicitly by RAISE statements.

What keyword in Python raises exception?

The raise keyword is used to raise an exception. You can define what kind of error to raise, and the text to print to the user.

How do you catch an unknown exception in Python?

Try and except statements are used to catch and handle exceptions in Python. Statements that can raise exceptions are kept inside the try clause and the statements that handle the exception are written inside except clause.


2 Answers

If you have completely separate tasks to execute depending on which function failed, as your code seems to show, then separate try/exec blocks, as the existing answers suggest, may be better (though you may probably need to skip the second part if the first one has failed).

If you have many things that you need to do in either case, and only a little amount of work that depends on which function failed, then separating might create a lot of duplication and repetition so the form you suggested may well be preferable. The traceback module in Python's standard library can help in this case:

import os, sys, traceback

try:
   os.mkdir('/valid_created_dir')
   os.listdir('/invalid_path')
except OSError, msg:
   tb = sys.exc_info()[-1]
   stk = traceback.extract_tb(tb, 1)
   fname = stk[0][2]
   print 'The failing function was', fname

Of course instead of the print you'll use if checks to decide exactly what processing to do.

like image 57
Alex Martelli Avatar answered Sep 28 '22 17:09

Alex Martelli


Wrap in "try/catch" each function individually.

try:
   os.mkdir('/valid_created_dir')
except Exception,e:
   ## doing something,
   ## quite probably skipping the next try statement

try:
   os.listdir('/invalid_path')
except OSError, msg:
   ## do something 

This will help readability/comprehension anyways.

like image 26
jldupont Avatar answered Sep 28 '22 16:09

jldupont