Home > Blockchain >  Catch an exception if no debugger is attached
Catch an exception if no debugger is attached

Time:11-27

Is it possible to catch an exception in Python only if a debugger is not attached, and bypass it to the debugger otherwise?

An equivalent C# code:

// Entry point
try
{
    ...
}
catch (Exception e) when (!Debugger.IsAttached)
{
    MessageBox.Show(e);
}

CodePudding user response:

You could check if you are having a Debugger. The question how that is done was awnsered here: How to detect that Python code is being executed through the debugger?

if not you gould pass the error thorug by using raise and the e object.

try:
    pass
except Exception as e:
    if !Debugger.IsAttached:
        MessageBox.Show(e);
    else:
        raise e

More on that you could find here: How to re-raise an exception in nested try/except blocks?

  • Related