UNIT III Errors and Exceptions in Python
UNIT III Errors and Exceptions in Python
Errors are problems in a program that causes the program to stop its execution.
a) Syntax errors: errors due to when proper syntax of the language is not followed.
Print(a)
c) Logical errors: errors due to the fact that the specification is not followed.
Incorrect behaviour of output.
The average of the numbers [10, 20, 30, 40, 50] should be 30 .
Actual Output: The program will output The average is: 29.0 .
No Syntax Error: The code runs successfully without any syntax errors.
Unexpected Output: The program produces output that is different from what
is expected.
Difficult to Detect: Logical errors are harder to identify and fix compared to
syntax errors because the program appears to run correctly.
Varied Causes: They can arise from incorrect assumptions, faulty logic,
improper use of operators, or incorrect sequence of instructions.
1. Compile time errors: syntax errors and static semantic errors indicated by the
compiler.
2. Runtime errors: dynamic semantic errors, and logical errors, that cannot be detected
by the compiler (debugging).
Exceptions are raised when some internal events change the program’s normal flow.
Errors that occur at runtime (after passing the syntax test) are called exceptions or logical
errors.
For instance, they occur when we
try to open a file(for reading) that does not exist (FileNotFoundError)
try to divide a number by zero (ZeroDivisionError)
try to import a module that does not exist (ImportError) and so on.
divide_numbers = 7 / 0print(divide_numbers)
Output
Exception Description
EOFError input() hits an end-of-file condition (EOF) without reading any data