Exceptions in Python

Errors in a Python Program In general, we can classify errors in a program into one of these three types:

  • Compile-time errors
  • Runtime errors
  • Logical errors
Compile-Time Errors

These are syntactical errors found in the code, due to which a program fails to compile. For example, forgetting a colon in the statements like if, while, for, def, etc. will result in compile-time error. Such errors are detected by Python compiler and the line number along with error description is displayed by the Python compiler.

Runtime Errors

When PVM cannot execute the byte code, it flags runtime error. For example, insufficient memory to store something or inability of the PVM to execute some statement come under runtime errors. Runtime errors are not detected by the Python compiler. They are detected by the PVM, only at runtime.

Logical Errors

These errors depict flaws in the logic of the program. The programmer might be using a wrong formula or the design of the program itself is wrong. Logical errors are not detected either by Python compiler or PVM. The programmer is solely responsible for them.

sal = sal * 15/100
sal = sal + sal * 15/100

A Python program to understand the effect of an exception.

So, when there is an error in a program, due to its sudden termination, the following things can be suspected:

  • The important data in the files or databases used in the program may be lost.
  • The software may be corrupted.
  • The program abruptly terminates giving error message to the user making the user losing trust in the software.

Hence, it is the duty of the programmer to handle the errors. Please understand that we cannot handle all errors. We can handle only some types of errors which are called exceptions.