# PEP 760 – No More Bare Excepts

**URL:** <https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182>\
**Category:** PEPs\
**Created:** [October 9, 2024, 12:38pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182 "2024-10-09T12:38:19Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pablogsal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pablogsal/32/32246_2.png) [@pablogsal](https://discuss.python.org/u/pablogsal)\
**Post date:** [October 9, 2024, 12:38pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/1 "2024-10-09T12:38:19Z")

</div>

‼ **Update: We have decided to withdraw the PEP as the consensus is that the benefits doesn’t justify the cost**

———-

👋 Hi everyone,

As the evil twin of [PEP 758: Allow `except` and `except*` expressions without parentheses](https://discuss.python.org/t/pep-758-allow-except-and-except-expressions-without-parentheses/66453), @brettcannon and me present you **PEP 760: No more bare excepts**.

This PEP proposes disallowing bare `except:` clauses in Python’s exception-handling syntax. Currently, Python allows catching all exceptions with a bare `except:` clause, which can lead to overly broad exception handling and mask important errors. This PEP suggests requiring explicit exception types in all except clauses, promoting more precise and intentional error handling.

Please **review the entire document** before commenting as it contains several aspects of how to address the deprecation, tooling, rejected ideas and more.

> **[PEP 760 – No More Bare Excepts | peps.python.org](https://peps.python.org/pep-0760/)**
>
> This PEP proposes disallowing bare except: clauses in Python’s exception-handling syntax. Currently, Python allows catching all exceptions with a bare except: clause, which can lead to overly broad exception handling and mask important errors. This...

Vote in this poll:

> [@PEP 760 – No More Bare Excepts](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/21):
>
> Ok, please vote in this friendly non-binding poll [poll](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/21)

---

<div class="post-metadata">

**Author:** ![barry](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/barry/32/42_2.png) [@barry](https://discuss.python.org/u/barry)\
**Post date:** [October 8, 2024, 12:35am UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/2 "2024-10-08T00:35:09Z")

</div>

> [@PEP 758: Allow \`except\` and \`except\*\` expressions without parentheses](https://discuss.python.org/t/pep-758-allow-except-and-except-expressions-without-parentheses/66453/33):
>
> Bare except with an unconditional raise is actually OK (and the stdlib has quite a few transactional APIs with rollback semantics)

I know this is off-topic for _this_ thread, but I raised this as an issue with the PEP 760 PR (the PEP itself hasn’t been published yet). I haven’t had time to look at the response from the PEP authors, but I would really like to preserve bare-`except` with an unconditional `raise` use case. If that can’t be done, I suggested an alias for `BaseException` called `AnyException` so at least the modified code would read better:

```python
try:
    something()
except:
     transaction.abort()
     raise
else:
     transaction.commit()

```

would become

```python
try:
    something()
except AnyException:
     transaction.abort()
     raise
else:
     transaction.commit()

```

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [October 8, 2024, 10:03pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/3 "2024-10-08T22:03:41Z")

</div>

> [@barry](#):
>
> I would really like to preserve bare-`except` with an unconditional `raise` use case. If that can’t be done, I suggested an alias for `BaseException` called `AnyException` so at least the modified code would read better

Can I ask why? I don’t find that any clearer than `except BaseException`, and if you are using that pattern I feel you should understand what you’re doing. As such, I don’t think learning what `BaseException` means is too much to ask.

> [@PEP 758: Allow \`except\` and \`except\*\` expressions without parentheses](https://discuss.python.org/t/pep-758-allow-except-and-except-expressions-without-parentheses/66453/40):
>
> My previous understanding is that what makes a tuple is the comma, not the parentheses.

It’s the comma _in the appropriate contex_; not every comma makes a tuple, but you can’t syntactically make a tuple without a comma.

---

<div class="post-metadata">

**Author:** ![barry](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/barry/32/42_2.png) [@barry](https://discuss.python.org/u/barry)\
**Post date:** [October 8, 2024, 10:32pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/4 "2024-10-08T22:32:44Z")

</div>

> [@brettcannon](#):
>
> Can I ask why? I don’t find that any clearer than `except BaseException`, and if you are using that pattern I feel you should understand what you’re doing. As such, I don’t think learning what `BaseException` means is too much to ask.

`BaseException` exposes the fact that it’s the root class in the exception hierarchy, and you have to connect that mentally to the fact that a) Python requires all exceptions to derive from it, and b) that pattern means “catch any exception”. The latter just makes that obvious. It’s obvious to _us_ of course either way.

For me, I’d still prefer to allow a bare `except` if there’s an explicit empty `raise` in the body.

---

<div class="post-metadata">

**Author:** ![iritkatriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/iritkatriel/32/2615_2.png) [@iritkatriel](https://discuss.python.org/u/iritkatriel)\
**Post date:** [October 8, 2024, 11:04pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/5 "2024-10-08T23:04:26Z")

</div>

> [@barry](#):
>
> For me, I’d still prefer to allow a bare `except` if there’s an explicit empty `raise` in the body.

Ideally if you want to intercept an exception and pass it through, you would do it will `finally`:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/python1/original/3X/3/e/3ed521c6dd358e7c5d85f5b8a53d7e0f60fb1dc1.png)

It would be nice to not have to use `sys`. What if we added an option to bind the exception in a `finally` block, as in:

```python
try:
    raise Exception("Hello")
finally e:
    if e is not None:
        e.add_note("World")

```

---

<div class="post-metadata">

**Author:** ![barry](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/barry/32/42_2.png) [@barry](https://discuss.python.org/u/barry)\
**Post date:** [October 8, 2024, 11:26pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/6 "2024-10-08T23:26:46Z")

</div>

I don’t think that handles the original use case: abort an in-progress transaction and re-raise when any exception occurs, and commit the transaction if no exception occurs.

---

<div class="post-metadata">

**Author:** ![iritkatriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/iritkatriel/32/2615_2.png) [@iritkatriel](https://discuss.python.org/u/iritkatriel)\
**Post date:** [October 8, 2024, 11:27pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/7 "2024-10-08T23:27:50Z")

</div>

```python
try:
    transaction
finally e:
    if e is None:
         commit
    else:
         abort

```

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [October 8, 2024, 11:39pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/8 "2024-10-08T23:39:11Z")

</div>

I do wonder, is there a point to removing bare excepts just to re-introduce them with new syntax in `finally` blocks?

But presumably this is a discussion for the PEP 760 thread, when it arrives.

---

<div class="post-metadata">

**Author:** ![iritkatriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/iritkatriel/32/2615_2.png) [@iritkatriel](https://discuss.python.org/u/iritkatriel)\
**Post date:** [October 8, 2024, 11:41pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/9 "2024-10-08T23:41:13Z")

</div>

The finally block is not a bare except. It doesn’t suppress exceptions.

---

<div class="post-metadata">

**Author:** ![ncoghlan](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ncoghlan/32/14266_2.png) [@ncoghlan](https://discuss.python.org/u/ncoghlan)\
**Post date:** [October 9, 2024, 12:25am UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/10 "2024-10-09T00:25:34Z")

</div>

Now there’s an interesting idea: don’t make bare except illegal, make it have an implicit raise at the end (and disallow return, break, and continue).

~~We’re well and truly off-topic for _this_ thread, though…~~ (comment was moved to a thread where it is on-topic)

---

<div class="post-metadata">

**Author:** ![iritkatriel](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/iritkatriel/32/2615_2.png) [@iritkatriel](https://discuss.python.org/u/iritkatriel)\
**Post date:** [October 9, 2024, 9:32am UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/11 "2024-10-09T09:32:21Z")

</div>

> [@ncoghlan](#):
>
> Now there’s an interesting idea: don’t make bare except illegal, make it have an implicit raise at the end (and disallow return, break, and continue).
> 
> We’re well and truly off-topic for _this_ thread, though…

As an alternative to my `finally` suggestion? I don’t think it would be coherent if `except` sometimes eats the exception and sometimes not.

Should we take this to another thread? Are the moderators able to move the comments on this subtopic to a new thread?

---

<div class="post-metadata">

**Author:** ![pablogsal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pablogsal/32/32246_2.png) [@pablogsal](https://discuss.python.org/u/pablogsal)\
**Post date:** [October 9, 2024, 12:40pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/12 "2024-10-09T12:40:30Z")

</div>

I have moved the relevant comments from [PEP 758: Allow `except` and `except*` expressions without parentheses - #55 by pablogsal](https://discuss.python.org/t/pep-758-allow-except-and-except-expressions-without-parentheses/66453/55) that pertain this discussion. Please flag any potential mistakes in the operation as discourse made this quite complex.

---

<div class="post-metadata">

**Author:** ![zuo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuo/32/11039_2.png) [@zuo](https://discuss.python.org/u/zuo)\
**Post date:** [October 9, 2024, 12:44pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/13 "2024-10-09T12:44:10Z")

</div>

I like the proposal, except that the _bare-except-with-unconditional-bare-raise_ pattern:

```python
except:
    <here cleanup / rollback / context-relevant log...>
    raise

```

– is IMHO too convenient (also when it comes to reading the code) to get rid of.

For me, the necessity to make it more verbose would be a clear loss.

---

<div class="post-metadata">

**Author:** ![encukou](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/encukou/32/2461_2.png) [@encukou](https://discuss.python.org/u/encukou)\
**Post date:** [October 9, 2024, 1:19pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/14 "2024-10-09T13:19:33Z")

</div>

+1 to keeping bare `except:` with `raise`.  
For better or worse, it’s correct, idiomatic code; the “Motivation” reasons don’t really apply to it.  
`pylint` and `ruff` allow this, and the the style guides their docs link to (PEP-8 and the [Google style guide](https://google.github.io/styleguide/pyguide.html#24-exceptions)) also mention that it’s OK. (`flake8` does flag it, though, and some of the other guides don’t list the exception.)  
And unlike `finally e:` or implicit raise, the existing syntax is compatible with Python 1.0+.

It would make the implementation harder, though.

---

<div class="post-metadata">

**Author:** ![pablogsal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pablogsal/32/32246_2.png) [@pablogsal](https://discuss.python.org/u/pablogsal)\
**Post date:** [October 9, 2024, 1:27pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/15 "2024-10-09T13:27:54Z")

</div>

> [@encukou](#):
>
> It would make the implementation harder, though.

Although it’s straightforward the implementation would be quite verbose as this implies doing an entire ast visitor just to detect raises below the bare except (in C).

The task becomes much simpler if we restrict it to detecting raises only at the same level of nesting but that feels somewhat odd.

I think this is an interesting case but you could skip all of this by just doing except BaseException and then all is good.

---

<div class="post-metadata">

**Author:** ![saaketp](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/saaketp/32/6703_2.png) [@saaketp](https://discuss.python.org/u/saaketp)\
**Post date:** [October 9, 2024, 1:32pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/16 "2024-10-09T13:32:43Z")

</div>

> [@encukou](#):
>
> or implicit raise, the existing syntax is compatible with Python 1.0+

Implicit raise is also compatible (or well just as compatible as mandatory raise inside bare except block).

---

<div class="post-metadata">

**Author:** ![hroncok](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hroncok/32/18696_2.png) [@hroncok](https://discuss.python.org/u/hroncok)\
**Post date:** [October 9, 2024, 1:32pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/17 "2024-10-09T13:32:48Z")

</div>

This breaks so much existing code. Does the benefit outweigh this? IMHO it’s OK to emit warnings left and right, but outright rejecting bare excepts after just 3 releases of deprecation period seems… not cool. Please, be more conservative with breaking backward compatibility of the basic language. This is not an API of one standard library module, this is the Python language itself.

---

<div class="post-metadata">

**Author:** ![zuo](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/zuo/32/11039_2.png) [@zuo](https://discuss.python.org/u/zuo)\
**Post date:** [October 9, 2024, 1:35pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/18 "2024-10-09T13:35:16Z")

</div>

> [@pablogsal](#):
>
> > [@encukou](#):
> >
> > It would make the implementation harder, though.
> 
> Although it’s straightforward the implementation would be quite verbose as this implies doing an entire ast visitor just to detect raises below the bare except (in C).
> 
> The task becomes much simpler if we restrict it to detecting raises only at the same level of nesting but that feels somewhat odd.

I think that a reasonable approach would be to require that a _bare `except`_ block:

- _must not_ contain `return`, `break` or `continue` (at any level of nesting, except in nested `def`s)
- _must_ contain `raise` at its top-level (open question: should only a bare `raise` or _any_ `raise...` be required?)

---

<div class="post-metadata">

**Author:** ![pablogsal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pablogsal/32/32246_2.png) [@pablogsal](https://discuss.python.org/u/pablogsal)\
**Post date:** [October 9, 2024, 1:45pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/19 "2024-10-09T13:45:43Z")

</div>

I think requiring this is reasonable but I am uncomfortable with the maintenance consequences of having to keep an entire visitor just for restricting this too much. The exception also feels slightly odd to communicate when really we can avoid all of this by just writing `except BaseException`

---

<div class="post-metadata">

**Author:** ![pablogsal](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/pablogsal/32/32246_2.png) [@pablogsal](https://discuss.python.org/u/pablogsal)\
**Post date:** [October 9, 2024, 1:46pm UTC](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182/20 "2024-10-09T13:46:57Z")

</div>

How many releases you think its reasonable?

[Next page](https://discuss.python.org/t/pep-760-no-more-bare-excepts/67182.md?page=2)
