I've been seeing a lot of this construct throughout my application:
def doSomething():
try:
# setup some variables
try:
# do something that could throw an OSError
except OSError as err:
# log and handle it
# do some more things
try:
# do something that could throw ValuError
except ValueError as err:
# log and handle it
except Exception as err:
# log the unexpected error
finally:
# close the logger
However, there are a number of posts that say nesting try-except blocks should be avoided, but I view that in this case:
try:
# throw
except:
try:
# throw
except:
try:
# throw
except:
...
In my former example, the entire method body is wrapped in a single try-except to ensure that in the event an unexpected error is encountered, it's not fatal and is logged before cleanup. Further, in the outermost try-catch, the nested blocks are handled and I continue on; I therefore fail to see how I would replace that with functions.