- Try and Else Block
- Pdb
- set trace method
SyntaxErrorNameErrorThis usually occurs when a variable is not defined or it hasn't been assigned yet.TypeErrorThis usually occurs when an operation or function is applied to a wrong type.IndexErrorThis occurs when we are trying to access an element in a list using invalid index.ValueErrorThis occurs when a built in operation or function receives an arguement that has the right type but an inappropiate valueKeyErrorThis occurs when a dictionary doesn't have a specific key.AttributeErrorThis occurs when a variable doesn't have an attribute.
- We can raise our own error exceptions
raise ValueError('invalid Error')- Lets say we have a simple function and we want it to have our own exception.
def color(text,color):
if type(text) is not str:
raise TypeError('Enter a valid String')
print(f'This {text} is using {color}.')- In python we can handle errors using
tryandexcept
try:
x
except ValueError:
print("there's a error")Else and Finally.
Finally would run no matter what.
# a small program
try:
x = int(input("Enter a number"))
except:
print("Enter a valid Number")
else:
print("Thanks for a valid number")
finally:
print("I will run no matter what hehehe")- In order to set a breakpoint we use
pdb.set_trace() - After setting a breakpoint then we can go line by line through our code.
lListnnext linepprintccontinue