Handling multiple exceptions in Python -
i want handle different exceptions in such way there common handling exception and additional specific handling subprocess.calledprocesserror
. this:
try: check_output(shlex.split(rsync_cmd)) except calledprocessexception e: # stuff # common exception handling except exception e: # same common handling
i don't want duplicate code end code:
try: check_output(shlex.split(rsync_cmd)) except exception e: if isinstance(e, calledprocesserror) , e.returncode == 24: # stuff # common handling
what's best practice run specific code 1 exception and run common code exceptions @ same time?
you may check error not against class, tuple of classes. in each branch may check specific properties.
try: n=0/0 except (ioerror, importerror) e: print(1) except (nameerror) e: print(2) traceback (most recent call last): file "<input>", line 2, in <module> n=0/0 zerodivisionerror: division 0
Comments
Post a Comment