Issue
When you just want to do a try-except without handling the exception, how do you do it in Python?
Is the following the right way to do it?
try:
shutil.rmtree(path)
except:
pass
Solution
try:
doSomething()
except Exception:
pass
or
try:
doSomething()
except:
pass
The difference is that the second one will also catch KeyboardInterrupt
, SystemExit
and stuff like that, which are derived directly from BaseException
, not Exception
.
See documentation for details:
However, it is generally bad practice to catch every error - see Why is "except: pass" a bad programming practice?
Answered By - vartec Answer Checked By - Marie Seifert (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.