I have an application that allows users to execute arbitrary IronPython scripts, embedded into the application, similar to Turtle example (http://www.voidspace.org.uk/ironpython/scripted_turtle.shtml). Scripting Engine executes within a try catch block, and that handles exceptions, unless the scripts start off a new thread, or do something else fancy, like load a loose XAML file and implement handlers behind XAML buttons, etc. Exceptions within such "fancy" code in IronPython scripts don't get handled by the C# try catch above, and bring down my application. I'd like to avoid this. Is there a way to either sandbox IronPython execution engine, or handle this scenario with some generic C# exception handling techniques? Putting try except into python scripts themselves is not an option in my case. So far I tried approaches 1 (AppDomain.CurrentDomain.UnhandledException) and 3 (Application.Current.DispatcherUnhandledException) from related SO question WPF global exception handler, but without success (handler does not get called when exceptions happen).
Asked
Active
Viewed 51 times
0
-
If you are executing the scripts why not to execute your IronPython script that will execute client script. In you script you can handle exceptions and return proper info back to your WPF app. – Yarl Jan 25 '19 at 17:34
-
@Ucho are you suggesting to wrap client's IronPython script with my own IronPython snippet, that handles exceptions? I don't think that would handle the underlying issue of an exception thrown on a different thread, as the client script would still be free to do that (it can still do anything it wants to and I don't know how exactly it will create new threads, etc). – Joe Jan 25 '19 at 17:37
-
@Ucho Ah, okay but you app will no longer be terminated. If client script has no handling for parallel thread exceptions you can do nothing about it. At least using normal execution engine. – Yarl Jan 25 '19 at 17:42
-
1and to clarify further, all scripts run within same process as WPF app (they have to, because they interact with it's internals, just like the turtle). I have not tried it, but I have a feeling that wrapping a script within a script - will still bring down my C# app. – Joe Jan 25 '19 at 17:44
-
It was just though I don’t really know IronPython. If the exception come from interaction between WPF and IronPython I have no clue. If exception on separate thread brings down your application it’s weird. Try magic or wait for someone who knows. Sorry. – Yarl Jan 25 '19 at 17:52