A Level Computer Science

Exception Handling

Further Programming

Creating computer programs is a creative process and there are many examples of cool websites, games, databases etc.

One thing that we can never guarantee is that the user of our program will input the correct type of data. This may cause a runtime error and the program may crash.

For example, a program may ask a user to enter a number but the user enters text. A runtime error may be generated and the program will probably crash!

An exception has just occured! An exception is simply an unexpected and usually unwanted event eg entering abnormal data into a computer program.

So, in our computer programs we need to tell the program how to handle exceptions.

This is called exception handling.

sample scenario

A simple program asks a user to enter their height in meters and their weight in kilograms.

The program will calculate the user's Body Mass Index.

Here is the program written in Python as the BMICalculator class:

class BMICalculator:

def __init__(self,height,weight){

self.__height = height;

self.__weight = weight;

self.__bmi = self.__weight/self.__height**2;


def getBMI(self):

return self.__bmi;


Code the above class in Python.

Now let's write a program which constructs a BMICalculator object:

h = float(input("Enter your height in meters");

w = float(input("Enter your weight in kilograms");

calculator = BMICalculator(h,w);

result = calculator.getBMI();

print("Your BMI is ", result);


Code the program.

Test it with some normal data. Does it work as expected?

What happens if you enter some text instead of numbers for weight and height?

When you enter unexpected data, it will probably cause as exception. In fact, you probably saw something like this after your program crashed:

ValueError: could not convert string to float:

This is a type of exception. You might have seen this common exception when working with arrays:

IndexError: list index out of range

And here is another one when you try to process a variable that has not been declared yet:

NameError: name 'y' is not defined

All of these kinds of errors will cause your program to crash unless you have some exception handling code to handle the error.

Let's look at how to handle these kinds of exceptions.

Handling exceptions

Many computer programming languages have the keywords try and except.

The Try block allows a program to test a block of code for errors without crashing.

The Except block of code runs if there is an exception/error.

For example, let's run this code:


Your program will crash because the variable age has not been declared.

How about this:





If you are not sure how this works, type it into your IDE and run it. Does your program crash?

You can define as many Except blocks as you want. For example, you may want to display specific messages for specific kinds of errors:



except NameError:

print("age has not been declared")


print("Something else went wrong")

Note, if there are no errors in the try block, then none of the except blocks of code will execute.


There is another exception handling keyword, finally.

This block of code will be run regardless of an exception being raised.


age = int(input("How old are you?"))

ageInDays = age*365


except ValueError:

print("Value Error occurred")




print("End of program.")


Return to your BMI Calculator program.

Can you use try, except and finally to handle any exceptions raised?

Past Paper Questions