views:

115

answers:

4

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 ?

+8  A: 

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.

jldupont
depending on the details, the second `try... except` could possibly go into an `else:` block from the first one.
Andrew Jaffe
This changes the logic slightly - if the `mkdir` fails because the directory already exists it trys to do the `listdir` regardless.
gnibbler
Of course I didn't craft the whole solution, just the principle.
jldupont
+1  A: 

How about the simple solution:

try:
   os.mkdir('/valid_created_dir')
except OSError, msg:
   # it_is_mkdir_whow_raise_ane_xception:
   do some things

try:
   os.listdir('/invalid_path')
except OSError, msg:    
   # it_is_listdir_who_raise_ane_xception:
   do other things ..
Ned Batchelder
+4  A: 

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 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.

Alex Martelli
+1 but isn't is stk[0][3] that returns the failing function?
luc
@luc, that would be the source but only if available; quoting the docs at http://docs.python.org/library/traceback.html?highlight=traceback#traceback.extract_tb, """A “pre-processed” stack trace entry is a quadruple `(filename, line number, function name, text)` representing the information that is usually printed for a stack trace. The text is a string with leading and trailing whitespace stripped; if the source is not available it is None.""" As you see `function_name` is at index 2 in the tuple.
Alex Martelli
the try/except, try/except solution may be better, but in my case i need to handle, what function raise the exception, so this answer is the best one for me! thanks :)
boos
@boos, you're welcome!
Alex Martelli
@boos: how does using two try/ except blocks not tell you which function raised the exception?
Tom
@Tom: for sure! using two try/except block tell me what function raised the exception, but it's not what i want.take a look at the code i write up after the @Alex Martelli answerhttp://nopaste.voric.com/paste.php?f=4nhemg
boos
@Tom: i think the firts code is mode readable than this one: http://nopaste.voric.com/paste.php?f=gpipbi
boos
If you have slightly different behavior for exceptions in different places, you should be *nesting* exception handlers, not branching based on stack traces.
Glenn Maynard
@Glenn Maynard: something like this ? i dont like it http://nopaste.voric.com/paste.php?f=s53vtv
boos
A: 

Here's the clean approach: attach additional information to the exception where it happens, and then use it in a unified place:

import os, sys
def func():
    try:
       os.mkdir('/dir')
    except OSError, e:
        if e.errno != os.errno.EEXIST:
            e.action = "creating directory"
            raise

    try:
        os.listdir('/invalid_path')
    except OSError, e:
        e.action = "reading directory"
        raise

try:
    func()
except Exception, e:
    if getattr(e, "action", None):
        text = "Error %s: %s" % (e.action, e)
    else:
        text = str(e)
    sys.exit(text)

In practice, you'd want to create wrappers for functions like mkdir and listdir if you want to do this, rather than scattering small try/except blocks all over your code.

Normally, I don't find this level of detail in error messages so important (the Python message is usually plenty), but this is a clean way to do it.

Glenn Maynard