Before handling errors, you need to know what kinds there are and what Python does on its own when one occurs.
Two kinds of errors
Syntax error
Exception (run-time error)
When detected
While Python compiles the file, before any line runs
While the program runs
Cause
Code that breaks the language’s grammar: missing colon, unmatched bracket
Valid code meets an impossible situation: division by zero, missing file, wrong type
Example
print("Hi", (3+4], lst = [4;5;6]
3 / 0, int("22.5"), [1, 2][5]
Can the program catch it?
Not in the same file (the file never starts)
Yes, with try and except
A third kind, the logical error, produces wrong results without any message (for example using + where * was intended); only testing finds these.
Common built-in exceptions
Exception
Raised when
Example
ZeroDivisionError
Dividing by zero
3 / 0
IndexError
A sequence index is out of range
[2, 3, 4][5]
KeyError
A dictionary key is missing
{"a": 1}["b"]
NameError
A name is not defined
x + 3 with no x
TypeError
An operation gets an unsuitable type
'2' * '3'
ValueError
The type is right but the value is not acceptable
int('22.5')
AttributeError
An object has no such attribute
(5).append(1)
FileNotFoundError
A file to be opened for reading does not exist
open("missing.txt")
OverflowError
A float result is too large to represent
2.0 ** 10000
ImportError, ModuleNotFoundError
An import fails
import nosuchmodule
KeyboardInterrupt
The user presses Ctrl+C
while a long loop runs
The default exception handler
If an exception is not handled, Python’s default exception handler takes over: it prints a traceback (the chain of calls that led to the error, the line number, the exception type and its message) and ends the program. Any statements after the failing one do not run; this is called abnormal termination.
Key takeaways
Syntax errors are found before running; exceptions happen during running.
Learn the common exceptions: ZeroDivisionError, IndexError, KeyError, NameError, TypeError, ValueError, FileNotFoundError.
Unhandled exceptions print a traceback and end the program.
Read a traceback from the bottom up.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Explain the difference between a syntax error, an exception and a logical error, with one example each.
Show worked answer
A syntax error breaks Python’s grammar and is detected before the program runs, for example if x > 3 without the colon. An exception is a run-time error in grammatically correct code, raised when an operation cannot be performed, for example 10 / 0 raising ZeroDivisionError; it can be caught with try/except. A logical error produces no message but gives wrong results, for example computing an average as total / 2 when there are three values; it is found only by testing.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/exception-handling-in-python-programming/errors-and-the-default-exception-handler/ to read this page.