Python language basics 57: starting with exceptions
October 18, 2015 1 Comment
Introduction
In the previous post we revisited dictionaries in python. We saw how to update a dictionary by adding new items to it or removing one.
In this post we’ll start looking at something very different: exception handling.
Exceptions and errors
In previous posts we saw a couple of references to various errors. These were errors that came from executing python code. Here’s an example:
Consider the following set:
capitals = {"Stockholm", "Budapest", "Helsinki", "Copenhagen", "Oslo", "Paris"}
If you try to remove an object which is not in the set:
capitals.remove("Washington")
This will result in a key error:
KeyError: ‘Washington’
We say that an exception was thrown in the code. The exception type is KeyError. It’s a very common word to use with exceptions: to throw an exception. Another way of saying it is that an exception is raised, i.e. “to raise an exception“.
Note that the terms “exception” and “error” can be used interchangeably but some languages like Java have stricter definitions for them. In this thread on StackOverflow we find the following definition for an error:
“An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions.”
Don’t worry about things like “subclass” and “Throwable” if you’re not sure what they mean. The main point of this definition is that “errors” are serious exceptions that indicate a fundamental flaw in the program flow.
Another interesting term in the above definition is the word “catch”. It says that applications can catch exceptions. Catching an exception is the counterpart of throwing one. Think of exceptions like they are balls that your friend is throwing at you that you will try to catch. That’s exactly what an application should aim to do. Exceptions are almost inevitable in any serious piece of software and if they are not caught and handled properly then program execution stops and the application crashes. That’s certainly not a good strategy to build reliable software. It must have happened to you that some application on your smartphone/tablet simply closed down by itself and you had to restart it. Most often it’s caused by an exception that the application did not catch.
One last important term about exceptions that you should know is handle. If an application catches an exception and treats it in some way, e.g. printing an exception message on the screen for the user, then the application has “handled” the exception: it is a “handled exception”, an exception that was caught in code and handled in some way. This process is called “exception handling”. Exceptions that are not caught are “unhandled exceptions”. Those are the ones that will cause the application to crash. You’ll also hear the term “uncaught exception”.
OK, that’s enough for the foundations, we’ll see some basic exception handling in code in the next post.
Read all Python-related posts on this blog here.
Pingback: Python language basics 58: exception handling in code | Dinesh Ram Kali.