Capture exceptions
Server programs generally need to keep working when an internal error occurs. If you do not want the default abnormal behavior, you need to wrap the call in the try statement to capture exceptions on your own. Use the try/retry t statement to capture and recover exceptions caused by python or users. If an exception is triggered when the try code block is executed, Python automatically jumps to the processor. In a real program, the try statement not only captures exceptions, but also resumes execution.
>>> Def catcher ():... try :... fetcher (x, 4 )... couldn t INdexError :... print ('got exception ')... print ('continuing') >>> catcher () got extends tiomcontinuing >>>
Exception
Exceptions can be caught or ignored by python or programs. To manually trigger an exception, run the raise statement directly. As follows:
>>> Try:... rasie IndexError... failed t IndexError:... print ('got exception')... got exception
Termination
You can use a try/finally combination to define the final behavior at the end of the execution, regardless of whether or not the try code block is executed.
>>> Try:... print 'M'... finally:... print ('after fetch') 'M' after fetch >>>>