Exception Handling

Events detected during execution that interrupt the flow of the program.

Different types of Exception in Python

There are several built-in exceptions that can be raised when an error occurs during the execution of a program.

Try-Except Statements

Try-expect statements are used to catch and handle exceptions in python. Statements that can raise exceptions are kept inside the try clause and the statements that can handle the exceptions are written inside the except clause.

Error in python can be of two types i.e. syntax errors and exceptions. Errors are the problems in a program due to which the program will stop the execution. On the other hand, exceptions are raised when some internal event occurs which changes the normal flow of the programs.

Some Common exception errors are:

try: numerator = int(input("Enter the numerator: ")) denominator = int(input("Enter the denominator: ")) result = numerator / denominator print("Result: ", result) # except Exception: # print("Something went wrong!!!") except ZeroDivisionError: print("You cannot divide by 0") except ValueError: print("Enter only number please") except ZeroDivisionError as e: print(e) print("You cannot divide by 0") # we can also add the else statement else: print(result) finally: print("This statement will always being executed") """ Output: Enter the numerator: 10 Enter the denominator: a Enter only number please This statement will always being executed """

Try with else Statements

The code defines a function AbyB(a,b) that calculates variable c as the ((a+b)/(a-b)) and handles a potential ZeroDivisionError. It prints the results if no division by zero error. Calling AbyB(2.0,3.0) calculates and prints -5.0, while calling AbyB(3.0,3.0) attempts to divide by zero, resulting in a ZeroDivisionError, which is caught and "a/b results in 0" is printed.

def AbyB(a,b): try: c = ((a+b)/(a-b)) except ZeroDivisionError: print("a/b cannot be divided to each other") else: print(c) AbyB(2.0,3.0) AbyB(3.0,3.0) """ Output: -5.0 a/b cannot be divided to each other """

Finally Keyword

Python provides a keyword finally, which is always executed after the try and except blocks. The final block always executes after the normal termination of the try block or adter the try block terminated due to the exception.

Syntax:-

try: # some code except: # optional block # handling the exception(if required) else: # execute if no exception finally: # Some code .... (always executed)

The previous code attempts to perform integer division by zero, resulting in a ZeroDivisionError. It catches the exception and prints. "Can't divide by zero". Regardless of the exception, the finally block is executed and prints "This is always executed".

try: k = 5//10 print(k) except ZeroDivisionError: print("Error: Division by zero is not allowed") finally: print("This is walys executed") """ Output: 0 This is always executed """

Raising Exceptions

The raise statement allows the programmer to force a specific exception to occur. The sole argument in raise indicates the exception to be raised. This may be either exception class or exception instance.

try: raise NameError("Hi there!!!") except NameError: print("An error occurred!!!") raise """ Output: py", line 2, in <module> raise NameError("Hi there!!!") NameError: Hi there!!! """

The output of this will simply printed as "An exception" but a Runtime error will also occur in the laste due to raise statement in the last line. So, the output on your command line will look like the above written in curly braces.

Advantages of Exception Handling

Disdvantages of Exception Handling