top of page
  • Writer's picturePIYUSH LIMKAR

Python try except else finally raise


 

1. Asking for help in Python


help(Exception)

 

2. try & except


a.

Code written below will throw an error

a = 5 b = 0 print(a/b)

print('Continue')

ZeroDivisionError: division by zero


This error will stop the further execution of the code.

In order to continue the execution of the code we use "try except"


b.

a = 5 b = 0 try: print(a/b) except: print('ZeroDivisionError: division by zero') print('Continue')


Above code will show the exception block but it won't stop execution.


 

c.

a = [1, 2, 3]

print ("Fourth element = %d" %(a[3]))

IndexError: list index out of range

try: print ("Second element = %d" %(a[1])) print ("Fourth element = %d" %(a[3])) except: print ("An error occurred")

 

3. try except else


a.

def divide(x, y): try: result = x / y print("Yeah ! Your answer is :", result) except ZeroDivisionError: print("Sorry ! You are dividing by zero ") divide(3, 2) print() divide(3, 0)


b.

def divide(x, y): try: result = x / y except ZeroDivisionError: print("Sorry ! You are dividing by zero ") else: print("Yeah ! Your answer is :", result) divide(3, 2) print() divide(3, 0)

 

4. try except else finally


a.

def divide(x, y): try: result = x / y except ZeroDivisionError: print("Sorry ! You are dividing by zero ") else: print("Yeah ! Your answer is :", result) print('This is always executed') divide(3, 2) print() divide(3, 0)

 

b.

def divide(x, y): try: result = x / y except ZeroDivisionError: print("Sorry ! You are dividing by zero ") else: print("Yeah ! Your answer is :", result) finally: print('This is always executed') divide(3, 2) print() divide(3, 0)

 

5. Single try & Multiple except


a.

def fun(a): if a < 4: b = a/(a-3) print("Value of b = ", b) try: fun(3) except ZeroDivisionError: print("ZeroDivisionError Occurred and Handled") except NameError: print("NameError Occurred and Handled")

 

b.

def fun(a): if a < 4: b = a/(a-3) print("Value of b = ", b) try: fun(5) except ZeroDivisionError: print("ZeroDivisionError Occurred and Handled") except NameError: print("NameError Occurred and Handled")

 

c.

def fun(a): if a < 4: b = a/(a-3) print("Value of b = ", b) try: fun(3) fun(5) except ZeroDivisionError: print("ZeroDivisionError Occurred and Handled") except NameError: print("NameError Occurred and Handled")

 

d.

def fun(a): if a < 4: b = a/(a-3) print("Value of b = ", b) try: fun(5) fun(3) except ZeroDivisionError: print("ZeroDivisionError Occurred and Handled") except NameError: print("NameError Occurred and Handled")

 

6. Multiple try & Multiple except


def fun(a): if a < 4: b = a/(a-3) print("Value of b = ", b) try: fun(3) except ZeroDivisionError: print("\nZeroDivisionError Occurred and Handled\n") try: fun(5) except NameError: print("\nNameError Occurred and Handled\n")

 

7. Raise an error using raise


1. The raise statement allows the programmer to force a specific exception to occur. 2. The sole argument in raise indicates the exception to be raised. 3. This must be either an exception instance or an exception class (a class that derives from Exception).



try: raise NameError("Hi there") # Raise Error except NameError: print ("An exception")

 

8. Creating class to raise an error


class MyError(Exception): def __init__(self, message): self.message = message l = 0 try: if l == 0: raise MyError('Invalid Divisor!!!') except MyError as error: print(error.message)

 

 


Recent Posts

See All
bottom of page