python - Calling a hook function every time an Exception is raised -


let's want able log file every time exception raised, anywhere in program. don't want modify existing code.

of course, generalized being able insert hook every time exception raised.

would following code considered safe doing such thing?

class myexception(exception):      def my_hook(self):         print('---> my_hook() called');      def __init__(self, *args, **kwargs):         global backupexception;         self.my_hook();         return backupexception.__init__(self, *args, **kwargs);   def main():     global backupexception;     global exception;      backupexception = exception;     exception = myexception;      raise exception('contrived exception');   if __name__ == '__main__':     main(); 

if want log uncaught exceptions, use sys.excepthook.

i'm not sure see value of logging all raised exceptions, since lots of libraries raise/catch exceptions internally things won't care about.


Comments

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -