53

I'm writing an exception clause at the top level of a script, and I just want it to log whatever errors occur. Annoyingly, PyCharm complains if I just catch Exception.

import logging

logging.basicConfig()

try:
    raise RuntimeError('Bad stuff happened.')
except Exception:  # <= causes warning: Too broad exception clause
    logging.error('Failed.', exc_info=True)

Is there something wrong with this handler? If not, how can I tell PyCharm to shut up about it?

3
  • If you're looking to turn warnings off they can be found in the settings. I believe you can search for the exact errors you are looking to change. Commented Nov 24, 2016 at 0:36
  • Thanks, @AugustWilliams, but I just want to tell PyCharm that this try/except block is good. I still want it to complain about other blocks, in case they aren't logging or dealing with the exception properly.
    – Don Kirkby
    Commented Nov 24, 2016 at 18:09
  • I remember seeing somewhere about ignore this occurrence or something like that. Can't remember where this button was though, apologies. Commented Nov 26, 2016 at 18:19

4 Answers 4

67

From a comment by Joran: you can use # noinspection PyBroadException to tell PyCharm that you're OK with this exception clause. This is what I was originally looking for, but I missed the option to suppress the inspection in the suggestions menu.

import logging

logging.basicConfig()

# noinspection PyBroadException
try:
    raise RuntimeError('Bad stuff happened.')
except Exception:
    logging.error('Failed.', exc_info=True)

If you don't even want to log the exception, and you just want to suppress it without PyCharm complaining, there's a new feature in Python 3.4: contextlib.suppress().

import contextlib

with contextlib.suppress(Exception):
    raise RuntimeError('Bad stuff happened.')

That's equivalent to this:

try:
    raise RuntimeError('Bad stuff happened.')
except Exception:
    pass
4

Not sure about your PyCharm version (mine is 2019.2), but I strongly recommend disabling this PyCharm inspection in File> Settings> Editor> Inspections and type "too broad". In Python tab, deselect "too broad exceptions clauses" to avoid those. I believe this way PyCharm would show you the correct expression inspection

1
  • 3
    In PyCharm 2021.2.2 this option doesn't exist. I just realized "# noinspection PyBroadException" only helps if it is inserted right before each try statement Commented May 3, 2022 at 14:51
3

I am reluctant to turn off warnings as a matter of principle.

In the case presented, you know well what the exception is. It might be best to just be specific. For example:

try:
    raise RuntimeError("Oops")
except RuntimeError as e:
    print(e, "was handled")

will yield "Oops was handled".

If there are a couple of possible exceptions, you could use two except clauses. If there could be a multitude of possible exceptions, should one attempt to use a single try-block to handle everything? It might be better to reconsider the design!

1
  • 2
    Then again, there are cases where you do not know the errors in advance and still want to catch them. For example in the socket library: "In non-blocking mode, operations fail (with an error that is unfortunately system-dependent) if they cannot be completed immediately" Commented May 3, 2022 at 14:50
3

I found a hint in this closed feature request for PyCharm:

I suggest you to mark this inspection as 'okay' if the except block makes use of exception instance e somehow.

Because I'm logging with exc_info=True, I'm implicitly using the current exception object, but PyCharm doesn't know that. To make it explicit, I can pass the exception object to exc_info. Since Python 3.5, the logger methods have accepted an exception instance to report, as well as accepting any truthy value to report the current exception and stack trace in the log.

import logging

logging.basicConfig()

try:
    raise RuntimeError('Bad stuff happened.')
except Exception as e:
    logging.error('Failed.', exc_info=e)
4
  • 2
    That's going to be horribly misleading to anyone reading this code later. Commented Nov 23, 2016 at 23:58
  • 8
    you could also add # noinspection PyBroadException to right above the try: this would tell pycharm (and probably other IDE's that you know its too broad and you are ok with it) Commented Nov 24, 2016 at 0:01
  • Why wouldd this be horribly misleading? Looks like the logging module checks either isinstance(exc_info, BaseException) in which case it extracts the data from the exception, tuple in which case it assumes the exc_info is accurate, or truthy in which case it uses sys.exc_info(). Passing exc_info=True and exc_info=e should yield identical results, no? (logging.__init__ at line 1461) Commented Apr 9, 2020 at 14:48
  • Thanks, @Sam, I hadn't noticed that change to exc_info. I've updated the answer to explain it better.
    – Don Kirkby
    Commented Apr 9, 2020 at 18:02

Not the answer you're looking for? Browse other questions tagged or ask your own question.