pieand so on nowsuppose that something goes very wrong during the "bake the piestep perhaps the oven is brokenor perhaps our robot miscalculates its reach and spontaneously combusts clearlywe want to be able to jump to code that handles such states quickly as we have no hope of finishing the pizza task in such unusual caseswe might as well abandon the entire plan that' exactly what exceptions let you doyou can jump to an exception handler in single stepabandoning all function calls begun since the exception handler was entered code in the exception handler can then respond to the raised exception as appropriate (by calling the fire departmentfor instance!one way to think of an exception is as sort of structured "super go to an exception handler try statementleaves marker and executes some code somewhere further ahead in the programan exception is raised that makes python jump back to that markerabandoning any active functions that were called after the marker was left this protocol provides coherent way to respond to unusual events moreoverbecause python jumps to the handler statement immediatelyyour code is simpler--there is usually no need to check status codes after every call to function that could possibly fail exception roles in python programsexceptions are typically used for variety of purposes here are some of their most common roleserror handling python raises exceptions whenever it detects errors in programs at runtime you can catch and respond to the errors in your codeor ignore the exceptions that are raised if an error is ignoredpython' default exception-handling behavior kicks init stops the program and prints an error message if you don' want this default behaviorcode try statement to catch and recover from the exception--python will jump to your try handler when the error is detectedand your program will resume execution after the try event notification exceptions can also be used to signal valid conditions without you having to pass result flags around program or test them explicitly for instancea search routine might raise an exception on failurerather than returning an integer result code-and hoping that the code will never be valid resultspecial-case handling sometimes condition may occur so rarely that it' hard to justify convoluting your code to handle it in multiple places you can often eliminate special-case code by handling unusual cases in exception handlers in higher levels of your program an exception basics