13

The following code should be executed without stopping the debugger:

var engine = Python.CreateEngine(AppDomain.CurrentDomain);
var source = engine.CreateScriptSourceFromString("Foo.Do()");
var compiledCode = source.Compile(new PythonCompilerOptions { Optimized = true });

try
{
    compiledCode.Execute(
         engine.CreateScope(
            new Dictionary<string, object> { 
                                            { "Foo", new Foo() }
                                           }));

    MessageBox.Show("Executed without error");
}
catch (Exception ex)
{
    MessageBox.Show(string.Format("Error at execution: {0}", ex.Message));
}

Using this class:

public class Foo
{
    public void Do()
    {
        throw new InvalidOperationException("The debugger should not break here...");
    }
}

The script execution runs in a try-block to handle any exception. If I have code like 1 / 0 all works perfekt. The exception is created in Python (or in the engine) and my catch-block is called as expected without forcing the debugger to stop anywhere.

Calling try { new Foo().Do(); } catch {} in C# does also work without stopping the debugger.

But throwing an exception in C#-code that's invoked in python will force the debugger to stop at the throw new...-line.

I don't want the debugger to stop there.

I disabled the first-chance-exceptions in Debug/Exceptions but the debugger still stops at throw.

I can't use DebuggerStepThrough because in my working code the exception it not thrown in this method but deeper in the code. The code is also used out of C# and decorating all these methods with DebuggerStepThrough will make my C# debugger obsolete.

A solution is to disable User-unhandled exceptions in Debug/Exceptions too but I want to break at exceptions that aren't handled by the user so this is not an option.

What can I do to disable the first-chance-exception called out of python code that's executed in a try...catch block?

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Sebastian Schumann
  • 3,204
  • 19
  • 37
  • Does the Python code contain try/catch? Presumably it doesn't, now it does get to be a judgement call where the "best" place for the debugger break should be located. Somewhat predictably, that won't be at a place where the Python code is no longer runnable and the actual source of the exception is no longer visible. You'd have to delete the PDB file for that C# code to convince the debugger that it is not "My Code". – Hans Passant Jun 11 '15 at 10:36
  • Do you see any? No it only contains `Foo.Do()`. I couldn't delete the PDB's because there's a lot of other code that should be debugged normally. – Sebastian Schumann Jun 11 '15 at 11:06
  • I didn't see any, you didn't post your Python code. Was there any point in being snarky about it? – Hans Passant Jun 11 '15 at 11:08
  • Please have a look at my posted C# code: `engine.CreateScriptSourceFromString("Foo.Do()");` Here it is... There's no hidden code... – Sebastian Schumann Jun 11 '15 at 11:09

1 Answers1

1

Most likely, you have a default option checked in your Debugging settings that's causing this:

enter image description here

Make sure that box is unchecked and you should be good to go. Hopefully that helps!

outbred
  • 1,028
  • 8
  • 13