Catch an exception if no debugger is attached

Question:

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);
}
Asked By: Dmitry Fedorkov

||

Answers:

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?

Answered By: NameVergessen
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.