Python language basics 57: starting with exceptions

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.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to Python language basics 57: starting with exceptions

  1. Pingback: Python language basics 58: exception handling in code | Dinesh Ram Kali.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

My goal with this blog is to offend everyone in the world at least once with my words… so no one has a reason to have a heightened sense of themselves. We are all ignorant, we are all found wanting, we are all bad people sometimes.

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: