Embed presentation
Download as PDF, PPTX





![ErrorsRuntime - 2What? When PVM cannot execute the byte code, it flags runtime errorExample Insufficient memory to store something or inability of the PVM toexecute some statement come under runtime errorsProgram Output#Accessing the item beyond the arrayboundslst = ["A", "B", "C"]print(lst[3])py 2.1_runtime_errors.pyTraceback (most recent call last):File "2.1_runtime_errors.py", line 5, in <module>print(lst[3])IndexError: list index out of range](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-6-2048.jpg&f=jpg&w=240)

![ErrorsLogical-2What? These errors depicts flaws in the logic of the programExample Usage of wrong formulasProgram Output#1. Open the filef = open("myfile", "w")#Accept a, b, store the result of a/b into the filea, b = [int(x) for x in input("Enter two number: ").split()]c = a / b#Write the result into the filef.write("Writing %d into myfile" % c)#Close the filef.close()print("File closed")py 4_effect_of_exception.pyEnter two number: 10 0Traceback (most recent call last):File "4_effect_of_exception.py", line 8, in<module>c = a / bZeroDivisionError: division by zero](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-8-2048.jpg&f=jpg&w=240)


![ExceptionsException HandlingThe purpose of handling errors is to make program robustStep-1 try:statements#To handle the ZeroDivisionError Exceptiontry:f = open("myfile", "w")a, b = [int(x) for x in input("Enter two numbers: ").split()]c = a / bf.write("Writing %d into myfile" % c)Step-2 except exeptionname:statementsexcept ZeroDivisionError:print("Divide by Zero Error")print("Don't enter zero as input")Step-3 finally:statementsfinally:f.close()print("Myfile closed")](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-11-2048.jpg&f=jpg&w=240)
![ExceptionsProgram#To handle the ZeroDivisionError Exception#An Exception handling Exampletry:f = open("myfile", "w")a, b = [int(x) for x in input("Enter two numbers: ").split()]c = a / bf.write("Writing %d into myfile" % c)except ZeroDivisionError:print("Divide by Zero Error")print("Don't enter zero as input")finally:f.close()print("Myfile closed")Output:py 5_exception_handling.pyEnter two numbers: 10 0Divide by Zero ErrorDon't enter zero as inputMyfile closed](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-12-2048.jpg&f=jpg&w=240)




![ExceptionsTypes: Program-3#Example for two exceptions#A function to find the total and average of list elementsdef avg(list):tot = 0for x in list:tot += xavg = tot / len(list)return tot.avg#Call avg() and pass the listtry:t, a = avg([1, 2, 3, 4, 5, 'a'])#t, a = avg([]) #Will give ZeroDivisionErrorprint("Total = {}, Average = {}". format(t, a))except TypeError:print("Type Error: Pls provide the numbers")except ZeroDivisionError:print("ZeroDivisionError, Pls do not give empty list")Output:Run-1:Type Error: Pls provide the numbersRun-2:ZeroDivisionError, Pls do not give empty list](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-17-2048.jpg&f=jpg&w=240)

![ExceptionsTypes: Program-3A#Example for two exceptions#A function to find the total and average of list elementsdef avg(list):tot = 0for x in list:tot += xavg = tot / len(list)return tot.avg#Call avg() and pass the listtry:t, a = avg([1, 2, 3, 4, 5, 'a'])#t, a = avg([]) #Will give ZeroDivisionErrorprint("Total = {}, Average = {}". format(t, a))except (TypeError, ZeroDivisionError):print("Type Error / ZeroDivisionError”)Output:Run-1:Type Error / ZeroDivisionErrorRun-2:Type Error / ZeroDivisionError](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-19-2048.jpg&f=jpg&w=240)






The document discusses different types of errors in programming:1. Compile-time errors occur due to syntax errors in the code and prevent the program from compiling. Examples include missing colons or incorrect indentation. 2. Runtime errors occur when the Python virtual machine (PVM) cannot execute the bytecode. Examples include type errors during operations or accessing elements beyond the bounds of a list.3. Logical errors stem from flaws in the program's logic, like using an incorrect formula. Exception handling allows programmers to anticipate and handle errors gracefully. The try/except blocks allow specific code to handle exceptions of a given type. Finally blocks ensure code is executed after the try/except blocks complete.





![ErrorsRuntime - 2What? When PVM cannot execute the byte code, it flags runtime errorExample Insufficient memory to store something or inability of the PVM toexecute some statement come under runtime errorsProgram Output#Accessing the item beyond the arrayboundslst = ["A", "B", "C"]print(lst[3])py 2.1_runtime_errors.pyTraceback (most recent call last):File "2.1_runtime_errors.py", line 5, in <module>print(lst[3])IndexError: list index out of range](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-6-2048.jpg&f=jpg&w=240)

![ErrorsLogical-2What? These errors depicts flaws in the logic of the programExample Usage of wrong formulasProgram Output#1. Open the filef = open("myfile", "w")#Accept a, b, store the result of a/b into the filea, b = [int(x) for x in input("Enter two number: ").split()]c = a / b#Write the result into the filef.write("Writing %d into myfile" % c)#Close the filef.close()print("File closed")py 4_effect_of_exception.pyEnter two number: 10 0Traceback (most recent call last):File "4_effect_of_exception.py", line 8, in<module>c = a / bZeroDivisionError: division by zero](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-8-2048.jpg&f=jpg&w=240)


![ExceptionsException HandlingThe purpose of handling errors is to make program robustStep-1 try:statements#To handle the ZeroDivisionError Exceptiontry:f = open("myfile", "w")a, b = [int(x) for x in input("Enter two numbers: ").split()]c = a / bf.write("Writing %d into myfile" % c)Step-2 except exeptionname:statementsexcept ZeroDivisionError:print("Divide by Zero Error")print("Don't enter zero as input")Step-3 finally:statementsfinally:f.close()print("Myfile closed")](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-11-2048.jpg&f=jpg&w=240)
![ExceptionsProgram#To handle the ZeroDivisionError Exception#An Exception handling Exampletry:f = open("myfile", "w")a, b = [int(x) for x in input("Enter two numbers: ").split()]c = a / bf.write("Writing %d into myfile" % c)except ZeroDivisionError:print("Divide by Zero Error")print("Don't enter zero as input")finally:f.close()print("Myfile closed")Output:py 5_exception_handling.pyEnter two numbers: 10 0Divide by Zero ErrorDon't enter zero as inputMyfile closed](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-12-2048.jpg&f=jpg&w=240)




![ExceptionsTypes: Program-3#Example for two exceptions#A function to find the total and average of list elementsdef avg(list):tot = 0for x in list:tot += xavg = tot / len(list)return tot.avg#Call avg() and pass the listtry:t, a = avg([1, 2, 3, 4, 5, 'a'])#t, a = avg([]) #Will give ZeroDivisionErrorprint("Total = {}, Average = {}". format(t, a))except TypeError:print("Type Error: Pls provide the numbers")except ZeroDivisionError:print("ZeroDivisionError, Pls do not give empty list")Output:Run-1:Type Error: Pls provide the numbersRun-2:ZeroDivisionError, Pls do not give empty list](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-17-2048.jpg&f=jpg&w=240)

![ExceptionsTypes: Program-3A#Example for two exceptions#A function to find the total and average of list elementsdef avg(list):tot = 0for x in list:tot += xavg = tot / len(list)return tot.avg#Call avg() and pass the listtry:t, a = avg([1, 2, 3, 4, 5, 'a'])#t, a = avg([]) #Will give ZeroDivisionErrorprint("Total = {}, Average = {}". format(t, a))except (TypeError, ZeroDivisionError):print("Type Error / ZeroDivisionError”)Output:Run-1:Type Error / ZeroDivisionErrorRun-2:Type Error / ZeroDivisionError](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2fexceptions-190509102255%2f75%2fPython-programming-Exceptions-19-2048.jpg&f=jpg&w=240)




