Errors in Python can be classified into
  • Compile-time errors
  • Runtime errors
  • Logical errors
Compile-time errors:
  • syntax errors can be classified as compile-time errors
  • For example, let us check the output for the below Python block.
x = 0
if x = 0
    print(x)

After compiling above program, output is as below:

 File "F:\python-progrm\Python-Tutorial-1.py", line 10
    if x = 0
         ^
SyntaxError: invalid syntax

We can observe that the line number is indicated with the error line

Run time errors:
  • errors reported by Python Virtual Machine (PVM) during the byte code execution
  • while syntax errors are compiler-time errors any errors which are syntactically correct and cannot be executed come under run time errors
  • Any statement which PVM cannot be executed shall be reported as run time error
  • Exception handlers are coded by programmers to handle the run time errors
  • for example, let us check the output for the below Python block.
ls = ['0','1']
print(ls[2])

When we run the above block, it reports below error.

File "F:\python-progrm\Python-Tutorial-1.py", line 10, in <module>
    print(ls[2])
IndexError: list index out of range

Logical errors:
  • These unlike run time and compiler errors have no relation to the PVM or Python compiler
  • These completely are a fault of the programmer because of which intended program output is not achieved
  • Example could be while calculating an average of 10 elements, user adding 10 elements but dividing only by 9 gives a wrong output.