Disabled first-chance-exception but debugger stopps within try...catch when using IronPython
Asked Answered
E

1

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?

Economize answered 9/6, 2015 at 9:27 Comment(4)
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".Yancey
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.Economize
I didn't see any, you didn't post your Python code. Was there any point in being snarky about it?Yancey
Please have a look at my posted C# code: engine.CreateScriptSourceFromString("Foo.Do()"); Here it is... There's no hidden code...Economize
C
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!

Cipher answered 14/7, 2015 at 16:17 Comment(1)
Great idea. Haven't checked that before. ... This checkbox is unchecked but the debugger still stops :-(Economize

© 2022 - 2024 — McMap. All rights reserved.