- Home
- Course Detail
regularpython@gmail.com
You are now watching:
Python Exceptions / of Python Exceptions Basic Examples
# # Exception Base class for all exceptions try: 1/0 except Exception as e: print(e) # # 1/0 print("hello") # # StopIteration Raised when the next() method of an iterator does not point to any object. a = [1,2,3,4] value = iter(a) print(next(value)) print(next(value)) print(next(value)) print(next(value)) print(next(value)) print(next(value)) try: a = [1,2,3,4] value = iter(a) print(next(value)) print(next(value)) print(next(value)) 1/0 except StopIteration: print("StopIteration Exception raised.") except Exception as e: print(e) else: print('There is no error') finally: print('i am in finally.') # SystemExit Raised by the sys.exit() function. import sys sys.exit("SystemExit Raised by the sys.exit() function.") print("hello world.") try: sys.exit("SystemExit Raised by the sys.exit() function.") except BaseException as e: print(e) print("hello world.") # #ArithmeticError Base class for all errors that occur for numeric calculation. number = 10/0 try: number = 10/0 except ArithmeticError as e: print(e) # #OverflowError Raised when a calculation exceeds maximum limit for a numeric type. import math # math.exp(1000) try: math.exp(1000) except ArithmeticError as e: print(e) # # FloatingPointError Raised when a floating point calculation fails. print(0.2*10**1000) # # ZeroDivisionError Raised when division or modulo by zero takes place for all numeric types. number = 10/0 try: number = 10/0 except ArithmeticError as e: print(e) # #AttributeError Raised in case of failure of attribute reference or assignment. # a = [1, 2, 3, 4, 5] # a.hi() try: a.hi() except AttributeError as e: print(e) # # EOFError Raised when there is no input from either the raw_input() or input() function and the end of file is reached. #ctrl+z try: name = input("What is your name? ") except EOFError as e: print(e) # # ImportError Raised when an import statement fails. import tutorial # # KeyboardInterrupt Raised when the user interrupts program execution, usually by pressing Ctrl+c. while True: print("hi") # # IndexError Raised when an index is not found in a sequence. a = [1,2,3,4] print(a[10]) try: print(a[10]) except IndexError as e: print(e) # # KeyError Raised when the specified key is not found in the dictionary. student = {"name":"tutorial", "age":22} print(student["rank"]) try: print(student["rank"]) except KeyError as e: print(e) # # NameError Raised when an identifier is not found in the local or global namespace. print(name) try: name except NameError as e: print(e) # # UnboundLocalError Raised when trying to access a local variable in a function or method but no value has been assigned to it. counter = 0 def increment(): counter += 1 increment() # # IOError Raised when an input/ output operation fails, such as the print statement or the open() function when trying to open a file that does not exist. try: open("fff") except Exception as e: print(e) # # SyntaxError Raised when there is an error in Python syntax. print("helll) # # IndentationError Raised when indentation is not specified properly. print("hello")