How to re-catch already caught exception in Python? -


i have code similar following:

try:     something() except derivedexception e:     if e.field1 == 'abc':         handle(e)     else:         # re-raise catch in next section! except baseexception e:     do_some_stuff(e) 

where derivedexception derived baseexception.

so, comment in code mentions - want re-raise exception inside of first except-section , catch again inside second except-section.

how that?

python's syntax provides no way continue 1 except block on same try. closest can 2 trys:

try:     try:         whatever()     except whatever e:         if dont_want_to_handle(e):             raise         handle(e) except broadercategory e:     handle_differently(e) 

personally, i'd use 1 except block , dispatch manually:

try:     whatever() except broadercategory e:     if isinstance(e, specifictype) , other_checks(e):         do_one_thing()     else:         do_something_else() 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -