set_custom_exc(exc_tuple, handler) Set a custom exception handler, which will be called if any of the exceptions in exc_tuple occur in the mainloop (specifically, in the run_code() method). Parameters ---------- exc_tuple : tuple of exception classe
(self, exc_tuple, handler)
| 1833 | self.InteractiveTB.set_mode(mode=self.xmode) |
| 1834 | |
| 1835 | def set_custom_exc(self, exc_tuple, handler): |
| 1836 | """set_custom_exc(exc_tuple, handler) |
| 1837 | |
| 1838 | Set a custom exception handler, which will be called if any of the |
| 1839 | exceptions in exc_tuple occur in the mainloop (specifically, in the |
| 1840 | run_code() method). |
| 1841 | |
| 1842 | Parameters |
| 1843 | ---------- |
| 1844 | |
| 1845 | exc_tuple : tuple of exception classes |
| 1846 | A *tuple* of exception classes, for which to call the defined |
| 1847 | handler. It is very important that you use a tuple, and NOT A |
| 1848 | LIST here, because of the way Python's except statement works. If |
| 1849 | you only want to trap a single exception, use a singleton tuple:: |
| 1850 | |
| 1851 | exc_tuple == (MyCustomException,) |
| 1852 | |
| 1853 | handler : callable |
| 1854 | handler must have the following signature:: |
| 1855 | |
| 1856 | def my_handler(self, etype, value, tb, tb_offset=None): |
| 1857 | ... |
| 1858 | return structured_traceback |
| 1859 | |
| 1860 | Your handler must return a structured traceback (a list of strings), |
| 1861 | or None. |
| 1862 | |
| 1863 | This will be made into an instance method (via types.MethodType) |
| 1864 | of IPython itself, and it will be called if any of the exceptions |
| 1865 | listed in the exc_tuple are caught. If the handler is None, an |
| 1866 | internal basic one is used, which just prints basic info. |
| 1867 | |
| 1868 | To protect IPython from crashes, if your handler ever raises an |
| 1869 | exception or returns an invalid result, it will be immediately |
| 1870 | disabled. |
| 1871 | |
| 1872 | WARNING: by putting in your own exception handler into IPython's main |
| 1873 | execution loop, you run a very good chance of nasty crashes. This |
| 1874 | facility should only be used if you really know what you are doing.""" |
| 1875 | if not isinstance(exc_tuple, tuple): |
| 1876 | raise TypeError("The custom exceptions must be given as a tuple.") |
| 1877 | |
| 1878 | def dummy_handler(self, etype, value, tb, tb_offset=None): |
| 1879 | print('*** Simple custom exception handler ***') |
| 1880 | print('Exception type :', etype) |
| 1881 | print('Exception value:', value) |
| 1882 | print('Traceback :', tb) |
| 1883 | |
| 1884 | def validate_stb(stb): |
| 1885 | """validate structured traceback return type |
| 1886 | |
| 1887 | return type of CustomTB *should* be a list of strings, but allow |
| 1888 | single strings or None, which are harmless. |
| 1889 | |
| 1890 | This function will *always* return a list of strings, |
| 1891 | and will raise a TypeError if stb is inappropriate. |
| 1892 | """ |
no outgoing calls