Exceptions

Exception handling is a mechanism for halting "normal" program flow and continuing at some surrounding context or code block.

The act of interrupting normal flow is called "raising" an exception. In some enclosing context the raised exception must be handled, which means control flow is transferred to an exception
handler. If an exception propagates up the call stack to the start of the program, then an unhandled exception will cause the program to terminate. An exception object, containing information about where and why an exceptional event occurred, is transported from the point at which the exception was raised to the exception handler so that the handler can interrogate the exception object and take appropriate action.

If you've used exceptions in other popular imperative languages like C++ or Java, then you've already got a good idea of how exceptions work in Python.

There have been long and tiresome debates over exactly what constitutes an "exceptional event", the core issue being that exceptionality is in reality a matter of degree (some things are more exceptional than others). This is problematic because programming languages impose a false
dichotomy by insisting that an event is either entirely exceptional or not at all exceptional.

The Python philosophy is at the liberal end of the spectrum when it comes to the use of exceptions. Exceptions are ubiquitous in Python, and it's crucial to understand how to handle them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset