If you do Python nested try-except, then each of this try-catch will handle different exceptions. Earlier we have seen exceptions at the same level. But, the objective of python nested try-catch is to find errors at different levels of code.
Python nested try-except example
Simple example code.
from math import sqrt, log
from numpy import arcsin
x = 5
message = None
x1 = sqrt(x)
message = "can't take sqrt"
x1 = log(x1 - 4)
message = "can't compute log"
x2 = arcsin(x1)
message = "Can't calculate arcsin"
How to use nested try/catch in python?
Answer: Use the try block with the inner
try-except clause (which is required).
try: # do something risky try: # do another risky thing except: # <-- this is required # handle the inner exception except Exception as exc: # handle outer exception
Do comment if you have any doubts or suggestions on this Python exception-handling topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.