Opening Hours :7AM to 9PM
In Python language exception handling can be achieved using try-except block construction.
Try and except blocks are the keywords in the Python language, it is used to convert System Defined Error Messages into User Friendly Error Messages very efficiently.
Try block always contains logic of the program or problematic code, except block contains User Friendly Error Messages.
Syntax:
Try: Logic of the code Except NameOfTheExcepton1 as object: User Friendly Message Except NameOfTheExcepton2 as object: User Friendly Message Except NameOfTheExcepton3 as object: User Friendly Message
Try: Logic of the code Except NameOfTheExcepton1 as object: User Friendly Message Try: Logic of the code Except NameOfTheExcepton1 as object: User Friendly Message
Try: Try: Logic of the code Except NameOfTheExcepton1 as object: User Friendly Message Except NameOfTheExcepton1 as object: try: Logic of the code Except NameOfTheExcepton1 as object: User Friendly Message
def myfun(): try: x=int(input("Enter x value")) y=int(input("Enter y value")) z=x/y print("Division Of Two Number Is ",z) except ZeroDivisionError as z: print("Denominator should not be zero") if __name__=="__main__": myfun()
def myfun(): try: x=int(input("Enter x value")) y=int(input("Enter y value")) z=x/y print("Division Of Two Number Is ",z) except ValueError as p: print("pls enter numbers only") except ZeroDivisionError as z: print("Denominator should not be zero") if __name__=="__main__": myfun()
finally: ... ... ...
def myfun(): try: x=int(input("Enter x value")) y=int(input("Enter y value")) z=x/y print("Division Of Two Number Is ",z) #print("Hi"); except ValueError as p: print("pls enter numbers only") except ZeroDivisionError as z: print("Denominator should not be zero") finally: print("Hi"); #try block statement if __name__=="__main__": myfun()
If any exception is designed by the user and it if used commonly by specific number of developers is known as user defined exception.
If any exception is unable to handle by the Python then those type of exceptions can be handled by the user by creating separate execution block.
Rules to design user defined exception:
1.Create a class and inherit Exception
2.call the exception class Using raise keyword
3.handle the user defined Exception using except block
Syntax:
Class classname(Exception): Pass: Def show(): Try: if(condition): Raise classname Except classname: User Friendly Message
class err(Exception): pass def myfun(): try: x=int(input("Enter x value")) y=int(input("Enter y value")) if(x<0 or y<0 or x>1000 or y>1000): raise err z=x/y print(z) except ValueError as v: print("Pls enter numbers only") except ZeroDivisionError as z: print("Denominator should not be zero") except err as e: print(" pls enter valid numbers") if __name__=="__main__": myfun()
class A(Exception): pass class nerr(A): pass class berr(A): pass def myfun(): try: x=int(input("Enter x value")) y=int(input("Enter y value")) if(x<0 or y<0): raise nerr elif x>1000 or y>1000: raise berr else: z=x/y print(z) except ValueError as p: print("pls enter numbers only") except ZeroDivisionError as z: print("Denominator should not be zero") except nerr as n: print("pls enterpositive numbers") except berr as b: print("Pls enter below 1000 number") if __name__=="__main__": myfun()