Handling both specific and general Python exceptions? -
i catch specific exception , handle accordingly - continue , perform generic handling other exceptions have to.
coming c background, have utilised gotos achieve desired effect.
this i'm doing , works fine:
try: output_var = some_magical_function() except integrityerror zde: integrity_error_handling() shared_exception_handling_function(zde) # error reporting except someotherexception soe: shared_exception_handling_function(soe) # same function above
tldr:
ie - there "pythonic" way of doing following:
try: output_var = some_magical_function() except integrityerror zde: integrity_error_handling() except allexceptions ae: # exceptions including intregityerror shared_exception_handling_function(ae) # error reporting
nb: aware of clause - isn't intended tidy-up (ie- closing files)·
you reraise exception, , handle generic case in outer handler of nested setup:
try: try: output_var = some_magical_function() except integrityerror zde: integrity_error_handling() raise except allexceptions ae: # exceptions including intregityerror shared_exception_handling_function(ae) # error reporting
the unqualified raise
statement re-raises current exception, integrityerror
exception thrown again handled allexceptions
handler.
the other path take test exception type:
try: output_var = some_magical_function() except allexceptions ae: # exceptions including intregityerror if isinstance(ae, integrityerror): integrity_error_handling() shared_exception_handling_function(ae) # error reporting
Comments
Post a Comment