xxxxxxxxxx
'''
Error in Python:
a. Syntax Error
- Occurs when Python encounters incorrect syntax (something it doesn't parse).
- Usually due to typos or now knowing Python well enough
ex.)
'''
def first: #SyntaxError , None = 1 #SyntaxError
'''
b. Name Error
- This occurs when a variable is not defined, i.e. it hasn't been assigned
ex.)
'''
test #NameError: name 'test' is not defined
'''
c. Type Error
- An operation or function is applied to the wrong type
- Python cannot interpret an operation on two data types
ex.)
'''
len(5) #TypeError: object of type 'int' has no len()
'''
d. Index Error
- Occurs when you try to access an element in a list using an
invalid index (i.e one that is outside the range of the list
or string)
ex.)
'''
list = ["hello"]
list[2] #INdexError: list index out of range
'''
e. Value Error
- This occurs when a built-in operation or function receives an
argument that has the right type but an inappropriate value.
ex.)
'''
int("foo") #ValueError: invalid literal for int() with base 10: 'foo'
'''
f. Key Error
- This occurs when dictionary doesn't have a specific key
ex.)
'''
d = {}
d["foo"] #KeyError: 'foo'
'''
g. Attribute Error
- This occurs when a variable doesn't have an attribute.
ex.)
'''
"awesome".foo #AttributeError: 'str' object has no attribute 'foo'
xxxxxxxxxx
try:
except SomeException:
tb = sys.exc_info()[2]
raise OtherException( ).with_traceback(tb)
type error in python
xxxxxxxxxx
geeky_list = ["Geeky", "GeeksforGeeks", "SuperGeek", "Geek"]
indices = [0, 1, "2", 3]
for i in range(len(indices)):
try:
print(geeky_list[indices[i]])
except TypeError:
print("TypeError: Check list of indices")