In: Computer Science
Respond to the following in a minimum of 175 words:
While debugging the code in Python there are two types of
errors:
1. Syntax error - This error occurs if there's any syntax issue in
the statements. specifically to Python as indentation matters here
as a new subpart of code
2. Runtime Error - This error occurs when there are no syntax
errors but program doesn't run as there are some violations. Few of
runtime errors are a) Divide by 0 error b) index out of bound error
c) name error variable not defined.
There's another type called Logic Error which could be some logical
issue in the algorithm or a different variable name used at a
particular place as human error.
Python uses try-except block to handle such errors.
Try block contains the code that could be prone to errors. so that
if any error occurs except block will check it.
Except block will define how to handle the occurred error. you
could mention specific error if needed or just write except
keyword. This will check for any error and execute this block when
try gets any error.
Code for try- except example.
a=5
b=0
try:
#Division by 0 will give error
quotient = a // b
print("Quotient is :", quotient)
#specify the error name
except ZeroDivisionError:
print("Divide by zero Error! ")
This except will only catch ZeroDivisionError
a=5
b=0
try:
#Division by 0 will give error
quotient = a // b
print("Quotient is :", quotient)
#specify the error name
except :
print("Divide by zero Error! ")
This will catch all errors possible