39

I've been using Visual Studio code for a long time with the Python extension.

Currently I have Visual Studio Code version 1.27.2, Python extension "ms-python.python" version 2018.8.0, python version 3.6.6, hosted by a Windows 10 1803 Enterprise Edition system.

Compared to the past, the latest version of Code / MS Python behaves differently in the debugging process.

In the past the execution of the following Python code, within Visual Studio Code, would not have generated any exceptions:

# Import exit from sys
from sys import exit as sys_exit
# Exit to system with errorlevel set to 1
sys_exit(1)
# Next instruction is never executed.
# Executing the script directly from the terminal in Windows sets the
# errorlevel to 1 and the script execution is terminated. 
print('unreachable code')

Now the result is a SystemExit exception when sys_exit(1) instruction is executed (VSCode screenshot).

Question: is there a way to disable this behavior and return to the previous one?

4
  • I looked at the VS Code source and this should be pretty easy for them to implement. They have this feature built into Visual Studio Code that you can configure the debugger to ignore specific exceptions types. Commented Feb 29, 2020 at 17:37
  • I have raised a feature request with the VS Code project here: github.com/microsoft/vscode/issues/91822 . Please give it a look. Commented Feb 29, 2020 at 18:05
  • Note: In theory, the 'unreachable code' print statement should not be reachable as by definition, sys.exit() should be a hard exit for the code and no further lines should run. Commented Feb 29, 2020 at 18:07
  • This is the real issue. Please upvote. It is still open: github.com/microsoft/vscode-python/issues/850 Commented Apr 15, 2020 at 13:16

2 Answers 2

39

I ran into something similar while debugging my flask application. My work-around was to uncheck "Uncaught Exceptions" in the breakpoint menu at the bottom of the debug window.

enter image description here

Sign up to request clarification or add additional context in comments.

10 Comments

It might be a workaround for some, but there is a problem if you are going to debug some tests: unchecking "Uncaught Exceptions" results in no tests ran. This behavior is considered a bug, so will be fixed: github.com/Microsoft/ptvsd/issues/982#issuecomment-435584288
Still hasn't been fixed as of May 2020.
sys.exit() is how you exit a Python script, is it not!? Can't understand why the heck it would be an exception. :(
2021 here, still not fixed. sys.exit(1) produces: Exception has occurred: SystemExit
2022 here, still not fixed
|
5

sys.exit can have a parameter. If you don't give it one it returns None. If anything besides 0 is returned its considered an error exit. So using this works

sys.exit(0)

2 Comments

Console applications returning non 0 exit code is not an Exception or necessarily an error.
Changing the behavior of an application to work around an IDE limitation is not a good solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.