Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Try/Except around whole module

I have a python module containing functions and a few classes. This module is basically used as a tool-set by several of my co-workers.

I want to set-up a sort of bug reporting system where anytime someone generates an exception that I don't handle, an email will be sent with information on the exception. This way I can continually improve the robustness of my code and the help-fullness of my own error messages. Is the best way to do this to just put a try/except block around the entire module?

like image 614
Darko Avatar asked Jun 01 '13 19:06

Darko


People also ask

Should I use try-except everywhere Python?

The reason to use try/except is when you have a code block to execute that will sometimes run correctly and sometimes not, depending on conditions you can't foresee at the time you're writing the code.

How do you trigger try-except in Python?

Python Try Catch Exception Example try: <--program code--> except: <--exception handling code--> <--program code--> ... Here, the program flow enters the “try” block. If there is an exception, the control jumps to the code in the “except” block.

Can you have multiple try-except Python?

In Python, try-except blocks can be used to catch and respond to one or multiple exceptions. In cases where a process raises more than one possible exception, they can all be handled using a single except clause.

Is try-except good practice?

WARNING! Try-except statements should never be used in place of good programming practice. For example, you should not code sloppily and then encase your program in a try-except statement until you have taken every measure you can think of to ensure that your function is working properly.


2 Answers

There are several reasons I think your approach might not be the best.

Sometimes exceptions should be thrown. For example, if I pass some stupid argument to a function, it should complain by throwing an exception. You don't want to get an email every time someone passes a string instead of an integer, etc. do you?

Besides, wrapping the entire thing in a try...except won't work, as that will only be catching exceptions that would occur during the definition of the classes/functions (when your module is loaded/imported). For example,

# Your python library
try:
   def foo():
       raise Exception('foo exception')
       return 42
except Exception as e:
   print 'Handled: ', e

# A consumer of your library
foo()

The exception is still uncaught.

like image 109
Jonathon Reinhart Avatar answered Sep 27 '22 21:09

Jonathon Reinhart


I guess you can make your own SelfMailingException and subclass it. Not that I would recommend this approach.

another option:

def raises(*exception_list):
    def wrap(f):
        def wrapped_f(*x, **y):
            try:
                f(*x, **y)
            except Exception as e:
                if not isinstance(e, tuple(exception_list)):
                    print('send mail')
                    # send mail
                raise
        return wrapped_f
    return wrap

usage:

@raises(MyException)
def foo(): 
    ...
like image 25
Elazar Avatar answered Sep 27 '22 21:09

Elazar