question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove bare except's and document broad exceptions.

See original GitHub issue

I’m speculating that Travis changed something because the build which is failing is just a trivial blacklist edit.

https://chat.stackexchange.com/transcript/message/40709387#40709387

flake8 chokes on bare except: clauses and I certainly agree that we should use a lot fewer of these – ideally none at all.

(The rest is a rant. You might want to stop reading here.)

If we have to have them, there should be a maximum of one per call tree, and as far up the call chain as possible. For example,

try:
  something
  try:
     something else
  except:
     pass
except:
  pass

… should probably be refactored to not use a bare except: in the inner scope, and possibly should not use try at all there as the exception will be captured by the caller anyway, and this sort of structure is extremely hard to understand and debug (and the real-life cases will be much more complex than the above).

As a rule of thumb, I like to put the ugliest code in main and keep the internal functions as clean and obvious as possible, and even have them raise errors when they need to (trusting that the ugliness of coping with the exception needs to be specific to each caller anyway, but we can keep things simple and elegant inside library functions).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
iBugcommented, Aug 7, 2018

So then, since it’s raised, I’ll leave this comment here.

As of revision ee2a44d4afcf9e31e96131608706ab6e4e8b631b, these broad exceptions exist.

wsl@MSI-GS70:~/proj/SmokeDetector $ grep -nF 'except Exception:' *.py
chatcommands.py:1418:    except Exception:  # I don't want to dig into ChatExchange
chatcommands.py:1441:        except Exception:  # I don't want to dig into ChatExchange
chatcommands.py:1500:    except Exception:  # I don't want to dig into ChatExchange
chatcommunicate.py:354:            except Exception:  # Everything else
excepthook.py:55:            except Exception:  # Broad exception makes sense here
fix_pickles.py:19:        except Exception:  # TODO: What could happen here?
metasmoke.py:311:            except Exception:  # TODO: What could happen here?
nocrash.py:87:    except Exception:  # Kinda sorta intended, used to be a pass so the BaseException below only catch program exits
spamhandling.py:161:    except Exception:  # TODO: What could happen here? We don't want unnecessary `except Exception`s
ws.py:177:    except Exception:  # TODO: What could happen here?

Every single non-capturing broad exception explained:

  • The three in chatcommands.py: The only statement inside the try block is a msg.delete() call. One needs to be familiar with ChatExchange to determine what could actually be raised here, but I’m not.
  • chatcommunicate.py: It’s appropriate here, to catch all exceptions raised from chat command functions (except CmdException which will have its message returned).
  • excepthook.py: Should be obvious that it’s appropriate here.
  • fix_pickles.py: I remember that it’s already replaced with OSError in the up-to-date master branch …?
  • metasmoke.py: Not sure what could be raised, left as-is
  • nocrash.py: It’s followed by a except BaseException so KeyboardInterrupts and SystemExits are correctly caught. ~Could be~ Already done in a more elegant way.
  • spamhandling.py: Looks like it’s intended (I left a wrong comment) so that any exception popped up in handle_spam() is handled by the exception hook.
  • 
    

There are another 12 except Exception as e’s that all seems to be correctly handled (logged and/or force reboot) and don’t belong to this topic.

To sum up, what needs to be replaced by more specific exceptions are the 3 in chatcommands.py and the one in metasmoke.py. This time it really should be the last of them.

2reactions
angussidneycommented, Aug 6, 2018

I would argue that this issue isn’t fully finished until all the except Exceptions are replaced with their specific Exceptions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to prevent "too broad exception" in this case?
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other ......
Read more >
bare-except / W0702 - Pylint 2.16.0-dev documentation
Description: Used when an except clause doesn't specify exceptions type to catch. Created by the exceptions checker.
Read more >
PEP 348 – Exception Reorganization for Python 3.0
In most existing Python 2.4 code, bare except clauses are too broad in the exceptions they catch. Typically only exceptions that signal an...
Read more >
broad-except suppressed when catching several ... - GitHub
Issue broad-except and bare-except even if the number of except handlers is different than 1. Closes issue #113.
Read more >
The Most Diabolical Python Antipattern
There are variants that amount to the same thing—saying except Exception: or ... If some code path simply must broadly catch all exceptions...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found