2.9.1.2 Finally and Else
Python Programming
The finally block always executes, regardless of whether an exception was raised. The else block, if used, runs only if no exceptions were raised in the try block. These features are useful for ensuring certain code always runs and for executing additional logic when no errors occur.
python
try :
number = int(input("Enter a number"))
result = 10 / number
except ZeroDivisionError:
print("You can’t divide by zero ! " )
except ValueError:
print ("Invalid input! Please enter a valid number.")
else :
print ("The result is", result)
finally :
print ("Execution completed.")