#!/usr/bin/env python # coding: utf-8 # # Polite error messages in IPython # # IPython allows registering a custom exception hook, # which lets you do whatever you want when an error occurs. # # If you want to hide the scary details of tracebacks for a bit, # you might use this. # # Here is an example showing a specific error message for NameErrors, # and showing just the error message instead of a traceback for the rest. # # Careful! Eliminating the traceback can make it a lot harder to find where the mistake is. # In[1]: def polite_errors(ip, etype, evalue, tb, tb_offset=None): if etype is NameError: print("Oops! looks like {}." " Did you forget something or make a typo?".format(evalue)) # elif other special errors you can make nice else: # fallback on just error type and message (no traceback) print("%s: %s" % (etype.__name__, evalue)) get_ipython().set_custom_exc((Exception,), polite_errors) # In[2]: undefined # In[3]: {}['a'] # In[4]: 1/0 # In[4]: def foo() pass