Attach debugger on error



I know about the possibility to stop the debugger on unhandled exceptions (python exception breakpoint) which works great when a code produces an unexpected error. However, this functionality requires my script to run in "debugging mode", which is much slower than "run mode" and may require hours before generating the error. How can I handle unexpected errors in run mode?

So far, my poor solution is to:
1. Run the script in run mode
2. Wait for the error
3. Attach the debugger from the console
4. Manually call the function where the error was generated
5. Finally debug

However, this solution doesn't work if the function itself triggering the error is slow. In that case, I'll have to wait for the function to run twice, once in "run mode" and once again with the debugger attached, and this is very impractical for scientific calculations.

Another possible solution is to write the entire script without using functions and run the code in "run mode", without using the debugger at all. If an error happens, I'll still be able to save my results using the console and investigate the cause of the error.

Is there a proper way to deal with this problem? Can I attach the debugger on unhandled error and then stop the debugger on the same line?

Thank you

Comment actions Permalink

Unfortunately, there is no such feature. As far as I know, it should work faster with Python >=3.6. Which version do you use?

Comment actions Permalink

Currently, I'm using Python 3.5.5, and my script is ~60% slower when debugging compared to running. If the slowdown is not that strong in Python >= 3.6 I will consider upgrading.

Would there be any downside in running everything in debug mode by default?

Thank you

Comment actions Permalink

>Would there be any downside in running everything in debug mode by default?

I don't think there are any problems with it. If it becomes faster after the update, you could just run debugger without breakpoints, and it will pause the program automatically in case of Exception.


Please sign in to leave a comment.