# Should a debugger treat exit(1) as an uncaught exception?

**URL:** <https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850>\
**Category:** Python Help\
**Created:** [July 10, 2024, 7:16pm UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850 "2024-07-10T19:16:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![9bit](https://avatars.discourse-cdn.com/v4/letter/9/439d5e/32.png) [@9bit](https://discuss.python.org/u/9bit)\
**Post date:** [July 10, 2024, 7:16pm UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850/1 "2024-07-10T19:16:45Z")

</div>

There is a popular VS Code extension called **debugpy** with 36,761,718 installs. This extensions treats `exit(1)` as an uncaught exceptions and halts debugging.

There is a [discussion](https://github.com/microsoft/debugpy/discussions/1591) going on about this at the debugpy repository, whether `exit(1)` should halt the debugger or not. I’m picking out the first and last post in the discussion:

Arguments against halting:

> The program program should exit without the debugger halting.
> 
> Exceptions that inherit from `exception.BaseException` should not fall under the category of “uncaught exceptions”, since they are “technically not an error”
> 
> […]
> 
> > exception SystemExit  
> > This exception is raised by the sys.exit() function. **It inherits from BaseException instead of Exception so that it is not accidentally caught by code that catches Exception**.

Arguments for halting:

> The Python documentation doesn’t say anything about how debuggers should behave, only about how user code should behave. It talks about “catching” exceptions, but debugger isn’t doing that - it is _reporting_ them. Thus this is not an issue of conformance. Debugger is reporting exceptions, and `SystemExit` is an exception. Whether to exclude it or not by default should be based on whether it makes sense for most scenarios or not. Given that a non-zero exit code is the standard way of reporting errors to the OS on all major platforms, thus it makes sense that it is reported by default.

I am curious what the Python developers thought when they designed the exception hierarchy and wrote the documentation. And if debuggers had any influence on these decisions, or not.

---

<div class="post-metadata">

**Author:** ![brass75](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brass75/32/19784_2.png) [@brass75](https://discuss.python.org/u/brass75)\
**Post date:** [July 10, 2024, 7:33pm UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850/2 "2024-07-10T19:33:07Z")

</div>

I can see advantages to halting at a non-zero exit code. By definition that means that something went wrong and it could be beneficial to examine the state at that time.

---

<div class="post-metadata">

**Author:** ![barry-scott](https://avatars.discourse-cdn.com/v4/letter/b/e9c0ed/32.png) [@barry-scott](https://discuss.python.org/u/barry-scott)\
**Post date:** [July 10, 2024, 9:13pm UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850/3 "2024-07-10T21:13:00Z")

</div>

> [@brass75](#):
>
> By definition that means that something went wrong

The status of a process on exit does can mean many things (wrong being only one posibility).  
It could mean the code of the program failed to run as intended.  
It could mean that the code worked as designed and needs to tell the call that something interesting happened.

For example the unix `test` command returns 0 if the test is true and 1 if the test is not true. The test command was not “wrong”.

---

<div class="post-metadata">

**Author:** ![JamesParrott](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamesparrott/32/10534_2.png) [@JamesParrott](https://discuss.python.org/u/JamesParrott)\
**Post date:** [July 11, 2024, 9:37am UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850/4 "2024-07-11T09:37:00Z")

</div>

I think the `debugpy` devs made a justifiable conservative design choice to catch everything. However I don’t know either, why debugging is then halted, at what in many cases is precisely the point of interest, even if the program has ended. Perhaps it would be a good extra feature, to allow a config setting to toggle this off?

Does it halt if the program explicitly raises a custom exception too? What does `pdb` do?

---

<div class="post-metadata">

**Author:** ![brass75](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brass75/32/19784_2.png) [@brass75](https://discuss.python.org/u/brass75)\
**Post date:** [July 11, 2024, 2:26pm UTC](https://discuss.python.org/t/should-a-debugger-treat-exit-1-as-an-uncaught-exception/57850/5 "2024-07-11T14:26:25Z")

</div>

Fair enough. Non-zero means “exited with error” and the error may not indicate something went wrong.
