Page 76 - IPP-11
P. 76
For example,
Fig. 3.22: Example of Runtime Error
The above statement is syntactically correct but won’t yield any output. Instead, it shall
generate an error as division of any number by 0 will result in an error upon execution and
illegal program termination. Some examples of Python runtime errors are:
• division by zero,
• performing an operation on incompatible types,
• using an identifier which has not been defined,
• accessing a list element, dictionary value or object attribute which doesn’t exist,
• trying to access a file which doesn’t exist.
CTM: Errors are exceptional, unusual and unexpected situations and they are never part of the normal
flow of a program.
In Python, such unusual situations are termed as Exceptions. Exceptions are usually runtime
errors.
3.15.3 Logical Errors
A logical error/bug (called semantic error) does not stop execution but the program behaves
incorrectly and produces undesired/wrong output. Since the program interprets successfully
even when logical errors are present in it, it is sometimes difficult to identify these errors.
Logical errors are the most difficult to fix. They occur when the program runs without crashing
but produces an incorrect result. The error is caused by a mistake in the program’s logic. You Python Programming Fundamentals (Additions)
won’t get an error message because no syntax or runtime error has occurred.
You will have to find the problem on your own by reviewing all the relevant parts of the code.
For example, if we wish to find the average of two numbers 10 and 12 and we write the code
as 10 + 12/2, it would run successfully and produce the result 16, which is wrong. The correct
code to find the average should have been (10 + 12)/2 to get the output as 11.
For example,
3.7