Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one have the vscode debugger halt even inside a try catch in python?

Tags:

Seeing the questions that are suggested as "similar" it seems most people want the opposite of what I want. What I want is that the debugger for Vs-code halts at the point of error even if inside a try catch (as it would usually do).

But instead what it does is not halt when inside a try catch like this one:

import traceback 

try:
    main() # has bugs I'm trying to debug
except Exception as e:
    send_email(traceback.format_exc())
    send_email(e)

I understand that this might be a weird thing to want & that vs-code's debugger is probably acting correctly (since my code it's telling it how to handle exceptions!) but I am having bugs that I want to debug rather to catch. In fact my outer try catch is just there because I am using a cluster that sends me emails when there is any bug and tells me about them. Otherwise I would not have a try catch at all around my main code.

Is there a way to tell vs-code to ignore my try catches when I am actually debugging?


An idea I just had as I was writing this was to change the sort of exceptions I catch...though during debugging I want it to always halt and when not debugging I want it to never halt and send me an e-mail with the bug.

Any ideas?


New error:

Exception has occurred: TypeError
catching classes that do not inherit from BaseException is not allowed

when using my EmptyException answer:

class EmptyError(Exception):
    def __init__(self):
        pass
like image 501
Charlie Parker Avatar asked May 03 '20 17:05

Charlie Parker


1 Answers

This might be a really dumb way to do it but when I running in debugging mode I have a flag that is set to true:

args.debug = True

then based on that flag I set the type of exception:

if args.debug:
    args.ExceptionType = Exception
else:
    args.ExceptionType = EmptyError

where EmptyError is a custom exception that doesn't do anything, it's just so that VS-code halts when I am running in debugging mode:

class EmptyError(Exception):
    def __init__(self):
        pass

Sort of dumb but I don't know anything better.

like image 188
Charlie Parker Avatar answered Sep 30 '22 19:09

Charlie Parker