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.

Forbid `raise MyExc()` in except handler without `from`

See original GitHub issue

Rule request

Thesis

This is the follow-up to #1184 issue. R100 rule is not covered in WPS.

Reasoning

This example does not see to catch problems with from absence:

try:
    some_dct['bar']
except KeyError:
    raise ValueError()

Also WPS329 doc cite such raise as example of correct code.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

2reactions
webknjazcommented, May 20, 2020

Sometimes you don’t want to process an exception on the current layer but want to log it there. In this case, you’d use a bare raise

def func2():
    try:
        do_something()
    except Exc1:
        log_it()
        raise


def func1():
    try:
        func2()
    except Exc1 as exc:
        actually_process_the_exception(exc)

Another case would be filtering out certain exception attributes. Like when an OSError happens, sometimes you want to only ignore one error code but you still want to pass-through other instances.

import errno

try:
    access_a_file()
except OSError as os_err:
    if os_err.errno != errno.ENOENT:  # we don't care if that thing does not exist but any other error mustn't be tollerated
        raise
    actually_process_the_exception(os_err)
1reaction
sobolevncommented, May 19, 2020

Then it can be closed, thanks everyone!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Copy rules from flake8-raise · Issue #1184 - GitHub
It has three rule that I absolutely love to have in 0.15 : R100 raise in except handler without from; R101 use bare...
Read more >
flake8-raise - PyPI
A flake8 plugin that finds that finds improvements for raise ... R100 raise in except handler without from. ... raise TypeError().
Read more >
Exception Handling in REALbasic - Thomas Tempelmann
What's the purpose of Exception Handling? Raising Exceptions; Handling Exceptions; Creating your own Exception types; Adding more information to Exceptions you ...
Read more >
Does raising an exception stop execution in Python? - Quora
Yes if the exception isn't captured by an except clause; and if it is captured then that exception is not re-raised or a...
Read more >
Raising exceptions when an exception is already present in ...
Answering to question 3, you can use: raise B('second') from None. Which will remove the exception A traceback. Traceback (most recent call ...
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