Errors and Debugging in Python


Error : When our application suddenly freezes for no apparent reason, it is known as an error.

Debugging : The process of finding errors in a program is known as debugging.

Three types of errors :

  1. Syntax Error :
    It is an error in the syntax.
    Eg : Incorrect indentation, Misspelling a keyword, leaving semi-colon(;), comma(,) etc.
    print 'hello' #SyntaxError : missing parenthesis
    L=[4; 7; 9] #SyntaxError : invalid syntax
  2. Logical Error/bug/semantic error :
    It does not stop execution but the program behaves incorrectly and produces wrong output.
    Eg : Using the wrong variable name, giving wrong operator precedence.
    #Finding average of two numbers
    a = 15
    b = 17
    c = a + b / 2 #logical error-operator precedence, it must be (a+b)/2
    print(c)
  3. Run-time Error :
    It usually results in abnormal program termination during execution.
    Eg : division by zero, trying to access file or elements that doesn’t exist.
    a = 15
    c = b / 0 #ZeroDivisionError : division by zero
    print(c)