You are the only Exception

--Originally published at Programming

Introduction to Exceptions

It’s been a long journey for some of you. Whether you’ve spent 10+ hours coding or just a couple, I’m pretty sure you are familiar with the error messages that the console displays when your code is wrong. So far you don’t care if the user enters a number when they are supposed to enter a string, but that’s from the past. It’s now time for us to take the next step and start handling exceptions. By now you should know that there are at least two important noticeable kinds of errors: syntax errors and exceptions.

Syntax Errors

Probably one of the most common kind of errors for the people that are still in the process of learning Python. The console will print a Syntax Error message showing you with an arrow ^ the earliest point in the line where the error was detected, followed by the number of the line so you know where to look.
1.png

2.png

Exceptions

A statement or an expression can be syntactically correct and yet, an error may occur when the program is executed. This is caused because of an exception, which is defined as an error occurred during the execution of a program. Exceptions come in different types, some types for the exceptions are:

  • ZeroDivisionError (Divide a number by 0)
  • TypeError(When an operation or function is applied to an object of inappropriate type)
  • ImportError(When an import statement fails to find the module)
  • IndexError(When a sequence substring is out of range)
  • KeyError(When a Dictionary key is not found in the set of existing keys)
  • ValueError()

Check out the documentation for the Python 3.5 Built-in Exceptions here

Handling Exceptions

Now that we know some types of exceptions, it’s time for us to learn how to handle them. Let’s take a look at the following example where the program asks

3.png
5.png
6.png
user for a number. Considering that the user enters a string “a” instead of a number the program will detect an error within the try clause, the rest of the clause will be skipped and the clause after the ValueError exception will be ran.

3.png

How it works

First of all, you need to know where to put your exception handler. For example, if you know that the user will mess something up, you are going to want to take some precautions.

  1. The code inside the try clause is executed.
  2. If no exceptions were found, the program will ignore the except clause and continue as if nothing’d happened.
  3. If the program runs into an exception, the rest of the try clause will be skipped and the except clause will be executed if and only if the exception occurred matches the exception named after the except keyword.
  4. If the exceptions do not match, then the exception will become an unhandled exception and an error message will be displayed in the console.

A try statement may have more than one except clause, to identify handlers for different exceptions. An except clause may name multiple exceptions as a tuple. For example:

except (ValueError, ZeroDivisionError,KeyError):

Else and Finally clauses

The else clause is optional. It is really useful when the try clause doesn’t raise any exceptions. The finally clause is also optional and it’s going to be executed no matter what. Basically the finally clause will always be executed.

Example

5.png

6.png