Page 101 - Information_Practice_Fliipbook_Class11
P. 101

4.8 Types of Errors

            Various  types  of  errors  may  be  generated  while  creating  or  executing  a  program.  The  process  of  identifying  and
            removing such errors is known as debugging. The errors are called bugs. The errors in a program broadly fall in three
            categories:
            ● Syntax Error
            ● Logical Errors
            ● Runtime Errors

            4.8.1 Syntax Errors

            Every programming language is described by a set of rules, collectively called the syntax of the  language. If a program
            violates a syntax rule, the interpreter/compiler will flash an error. Such errors are known as syntax errors. Thus, a Python
            interpreter will execute only those statements that are syntactically correct. When a  syntax error is encountered, the
            execution of the program stops, and the error message is displayed. The execution of the program resumes only after
            the  error is rectified. For example,

             >>> print("hello)
                 SyntaxError: unterminated string literal (detected at line 1)
            In the statement print("hello), the closing quotation marks are missing, leading to a syntax error. The Python
            interpreter noted the beginning of string, but could not find the closing quote marks. A syntax error must be removed
            before the Python interpreter can proceed.



                   Python Interpreter gives a brief description of each error, as seen in the above example.



            4.8.2 Logical Errors

            A logical error does not give the desired output, even though there is no syntax error in the program. Such errors are
            sometimes difficult to identify as all the statements in the program are executed successfully. When a logical error is
            encountered, often it helps to work backwards by examining the output produced by the program and looking for the
            cause of the error.

            For example, to calculate the percentage of marks obtained by a student, the expression should be
            percentage = marksObtained/maxMarks * 100
            However, if the programmer writes the formula as

            percentage = maxMarks/marksObtained * 100
            The statement being syntactically correct, will be executed but it will not give the correct output.

            4.8.3 Runtime errors

            As the name suggests, a runtime error occurs during the execution of the program. The statement is syntactically
            correct but cannot be executed by the interpreter. For example, while executing the statement
            result =  numerator/denominator
            if the value of denominator is 0 (zero), it will result in a runtime error (division by zero). Similarly, a runtime error will
            be generated, if the user enters a string instead of a number. Program 4.2 below illustrates the runtime error.











                                                                                        Data Types and Operators  87
   96   97   98   99   100   101   102   103   104   105   106