👉It is highly recommended to handle
exceptions.
👉The code which may raise an exception is
called risky code, and we have to take
risky code inside the try block.
👉The corresponding handling code we
have to take inside the except block.
Syntax:
try:
Risky Code
except XXX:
Handling code / Alternative Code
Example:without try-except:
1. print("stmt-1")
2. print(10/0)
3. print("stmt-3")
Output:
stmt-1
ZeroDivisionError: division by zero
Abnormal termination / Non-Graceful Termination
Example:with try-except:
1. print("stmt-1")
2. try:
3. print(10/0)
4. except ZeroDivisionError:
5. print(10/2)
6. print("stmt-3")
Output:
stmt-1
5.0
stmt-3
Normal termination / Graceful Termination
No comments:
Post a Comment
Thank you Very Much.For Given Comment