Is there a way to force an exception to be raised if there is a try catch that catches all exceptions?

Viewed 59

I need to force an exception to be raised outside a function that does this:

def foo(x):
   try:
     some_calculation(x)
   except:
     print("ignore exception")

Is there a way to override the catch-all inside foo? I would like to raise an exception inside some_calculation(x) that can be caught or detected outside foo.

FYI, foo is a third party function I have no control over.

1 Answers

No. Your options are:

  • submit a fix to the library maintainers
  • fork the library, and provide your own vendorised version
  • monkey patch the library on import

The last is perhaps the easiest and quickest to get up and running.

Example:

main.py

# before doing anything else import and patch the third party library
# we need to patch foo before anyone else has a chance to import or use it
import third_party_library

# based off of third_party_library version 1.2.3
# we only catch Exception rather than a bare except
def foo(x):
   try:
     some_calculation(x)
   except Exception:
     print("ignore exception")

third_party_library.foo = foo

# rest of program as usual
...

Things might be slightly more complicated than that if foo() is re-exported across several different modules (if the third party library has its own from <x> import foo statements. But if just requires monkey patching more attributes of the various re-exporting modules.

Technically it would be possible to force an exception to be raised, but it would involve setting an execution trace and forcing an exception to be thrown in the exception handling code of the foo(). It would be weird, the exception would appear to come from print("ignore exception") rather than some_calculation(x). So don't do that.

Related