Python Scripting - Week 9 Flashcards
Kinds of Exceptions in Python ?
TypeError “a”+2
ZeroDivisionError 5/0
ImportError from numpy import tree
ModuleNotFoundError import asd
KeyError a = dict{b=10,c=20}
print(a[25])
IndexError ‘hello’[10]
NameError print(basic)
How to catch all exceptions ?
def square():
while True:
value=input(“Enter a Value : Must be int - “)
try:
num=int(value)
return f”Square of ‘{num}’ is ‘{num *num}’”
except:
print(“Not an integer type , Please ENTER Again”)
How to print an error message on certain condition ?
x = 10
if x > 5:
raise Exception(‘x should not exceed 5. The value of x was: {}’.format(x))
How many types of errors are in python ?
In python we have two types of errors,
syntax error or an exception.
Write a code to handle multiple exceptions ?
try:
“hello”[20]
except ZeroDivisionError:
print(“This is an zero division error”)
except IndexError:
print(“Index Error”)
What is e in this statement ,
except IndexError as e
The “as e” part of the statement is used to assign the error message to a variable (in this case, “e”) so that it can be accessed and used later in the program
How to catch any type of exception error in variable