Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use atexit when exception is raised

Tags:

python

i have a scenario where some unknown exceptions may be raised during program execution and i can't except most of them and i want every time any exception will raise an email should send to me as exceptions cause program to terminate if not properly catch!

so i have read about python provide atexit module but it did not work with exceptions so my question is , is there any way to make atexit work with exceptions?? so every any exception raised and programs terminates it should send me a mail? thanks

like image 923
maq Avatar asked Jun 25 '15 20:06

maq


People also ask

How does Atexit work in Python?

atexit is a module in python which contains two functions register() and unregister(). The main role of this module is to perform clean up upon interpreter termination. Functions that are registered are automatically executed upon interpreter termination.

What happens when an exception is raised in a program in Python?

Python Exceptions. When a Python program meets an error, it stops the execution of the rest of the program. An error in Python might be either an error in the syntax of an expression or a Python exception.

What is try finally in Python?

The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error. The finally block lets you execute code, regardless of the result of the try- and except blocks.


1 Answers

Look at sys.excepthook. As its name suggests, it's a hook into the exception. You can use it to send you an email when exceptions are raised.

like image 105
DevShark Avatar answered Sep 28 '22 21:09

DevShark