In python to implement error handling we use try and except blocks whereas else clause is completely optional.
Error handling in python with else clause
We can use else block to check whether the code in try block is executed or not . The statements in else block only runs if the code in try block works.
Here’s an example for try with else clause in python,
try: #code which may give an exception x=int(input("Enter any number: ")) result=10/x except InfiniteError: #if x=0 we need to handle the error print("A number can't be divided with zero") else: #When no exceptions occurred this code will run print("Division: ",result)
From above code , we can tell that input number could be zero. We know that division of any number with zero gives an error to handle that we use try and except block. To tell the user that there is no error we use else block optionally.
Thank you reading this post…