Errors are common in any programming language, coming to python we can classify errors in a program into one of these three types:
- Compile-time errors
- Runtime errors
- Logical 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.Compile-time error example
#example for compile-time error x = 1 if x == 1 print('Where is colon?') #another compile-time error x = 10 if x--2==0: print(x,' is divisible by 2') print(x,' is even number')
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.Runtime error example
def concat(a, b): print(a+b) #callconcat() and pass arguments concat('Hai', 25) animal = ['Dog', 'Cat', 'Horse', 'Donkey'] print(animal)
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.
def increment(sal): sal = sal * 15/100 return sal #call increment() and pass salary sal = increment(5000.00) print('Incremented salary= --.2f' --sal)
sal = sal * 15/100
sal = sal + sal * 15/100Python Effect of Exception Example
#an exception example #open a file f = open("myfile", "w") #do some processing on the file #accept a, b values, store the result of a/b into the file a, b = [int(x) for x in input("Enter two numbers: ").split()] c = a/b f.write("writing --.2f into myfile" --c) #close the file f.close() print('File closed')
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.