import

sys

import

logging

import

tempfile

# You can use the standard Python logger to log exceptions.

# This one is a dummy one that only prints on the console.

logging

.

basicConfig

()

log

=

logging

.

getLogger

()

# Avoid erasing the previous exception handler.

# You should ALWAYS do this.

previous_hook

=

sys

.

excepthook

def

on_crash

(

type

,

value

,

tb

):

# Here you can do something right before the program crash.

# You could even log the stack trace in a file or send an email.

log

.

critical

(

"Arrrg"

,

exc_info

=

(

type

,

value

,

tb

)

)

if

previous_hook

is

sys

.

__excepthook__

:

# Here you can do something if the previous handler

# is the standard Python one.

print

(

'... yes, but not a standard arg !'

)

# You could reuse the previous handler if you wish:

# previous_hook(type, value, tb) # The default handler displays the traceback

sys

.

excepthook

=

on_crash

1

/

0