An exception is an anomalous or exceptional condition requiring special processing – often changing the normal flow of program execution.
Be careful not to confuse an exception with an error.
An error "indicates serious problems that a reasonable application should not try to catch." An exception "indicates conditions that a reasonable application might want to catch.".
We use the term "catch an exception" to catch exceptions and plan how the program should respond.
# here is some Python code with a deliberate error. There should be two apostrophe's for the end argument. print("hello world", end=') print("from Warsaw") print("=" * 25)
When we execute this code we see the following exception:
You can see there are four lines here:
- on the first line we see the name of the file being executed and the line number where the exception was thrown.
- on the second line we see the line of code where the exception was thrown
- on the third line we see a carat (^) pointing the place where the exception was thrown
- on the fourth line we see the description of the exception
If you google the exception (line 4) you can often find helpful information about the exception. For a list of common exceptions, please click here
These standards are used from the IB Computer Science Subject Guide
- Identify exceptions that need to be considered in a specified problem solution.
- IB Diploma Programme Computer science guide (first examinations 2014). Cardiff, Wales, United Kingdom: International Baccalaureate Organization. January 2012.