import exceptions python code example

Example 1: catch error python

import traceback

dict = {'a':3,'b':5,'c':8}
try:
  print(dict[q])
 
except:
  traceback.print_exc()
  
# This will trace you back to the line where everything went wrong. 
# So in this case you will get back line 5

Example 2: attribute error python

# If python gives that error , 
# It means that whatever module doesn't have that specific function
# Therefore it can't run that specific function
# Example (recreating the error)

class Thing(object):
    
    def func(self):
        print("Random function was ran from class Thing().")

some_object = Thing()
# This will run properly
some_object.func()
# This won't run and produce the AttributeError.
some_object.some_func()