2

I am trying to catch an exception that is not raised, just logged. Is there any way to do this?

def exampleFunction():
    try:
        x = 1 / 0
    except Exception:
        logging.exception('divide by 0 error here.')

try:
    exampleFunction()
except <condition based on if logging.exception was called>
    print('there was a divide by 0 error when I called exampleFunction()')
5
  • 3
    I sure hope there isn't. If you're interested in knowing whether an exception occurred, why does exampleFunction silently swallow that exception? Commented Sep 26, 2018 at 23:28
  • There's probably an awful way to do this with mock. Let me thing... Commented Sep 26, 2018 at 23:29
  • Basically, the exampleFunction just swallows the exception so it doesn't break any code that calls it by throwing a random exception (thinks the issue is minor/skippable). But in my specific case, I need to treat it as an exception Commented Sep 26, 2018 at 23:31
  • Can or can't you modify exampleFunction? Commented Sep 27, 2018 at 7:55
  • @Aran-Fey I have seen this in third party libs who assume that this is an internal error which nobody every wants to catch - unfortunately, they guessed wrong and I needed to catch that exception. Commented Sep 27, 2018 at 8:21

2 Answers 2

2

I assume that exampleFunction is in some horrible third party code that you do not own, otherwise there are lots of better alternatives.

What you can do:

import logging # make sure this is the FIRST import of logging
import horrible_third_party_lib

def catch_log(msg):
   # do something when this is called
old_logging = logging.exception
try:
    logging.exception=catch_log
    horrible_third_party_lib.exampleFunction()
finally:
    logging.exception=old_logging

You can then do everything you want in the function. It's horrible and sensitive to import order, but I have used this and it works (scikit learn does something similarly repugnant..)

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to use the function catch_log to actually throw an Exception? Basically, I want to be able to do something where I can raise an Exception in def catch_log and do an except Exception clause in-between the try: and finally:
Yes of course, just raise the exception you want to throw
When I tried raising an Exception in catch_log, it didn't actually end up being caught..
0

Question: I am trying to catch an exception
The Questions Title should be rewriten to "I am trying to reraise an exception"

Read Python Documentation#raise
Seems somewhat double work, but you can do it like that:

def exampleFunction():
    try:
        x = 1 / 0
    except Exception as e:
        print("logging.exception({})".format(e))
        raise

try:
    exampleFunction()
except ZeroDivisionError as e:
    print('there was a divide by 0 error when I called exampleFunction()')

Output:

logging.exception(division by zero)
there was a divide by 0 error when I called exampleFunction()

Tested with Python:3.5.3

3 Comments

Using raise at the end would be enough to reraise the last exception.
@metmirr: Please explaine, which end?
I mean this: except Exception as e: print('...'); raise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.