I'm trying to implement some error handling into my C# code for my .NET application. I feel like I'm missing something really obvious because I know there has to be a way to do this, but I just can't figure it out. I have a python script I call out to using gp.Execute()
. If there is an error in the python script and it does not complete, I cannot get the message from it, because any call like gp.GetMessages()
has to come after the gp.Execute()
statement, so it never gets hit if the script encounters an error and execution stops in my C# code. How do you get error messages from your scripts? I have also tried to put gp.GetMessages()
in the catch statement but the gp
object is considered out of scope there.
My current code:
try {
//stuff here
//Run PopulatePatches script
IGeoProcessor2 gp = new GeoProcessorClass();
gp.AddToolbox(@"C:\Users\mxxx\Desktop\GIS_Testing\HbtatTesting\HabitatMon.tbx");
IVariantArray parameters = new VarArrayClass();
parameters.Add(shapefile);
gp.Execute("PopulatePatches", parameters, null);
gp.GetMessages(ref sev) //never hits this line if python script fails
}
catch {
ViewBag.Message = "Failed!";
return View();
}
Lastly, I have gotten rid of the try/catch block and tried to just get the script to play through by doing this:
IGeoProcessorResult pResult = gp.Execute("PopulatePatches", parameters, null)
int severity = 2;
string messages = pResult.GetMessages(severity);
//I have also attempted the above line with just straight gp.GetMessages(severity)
//because I think result objects are null if the tool doesn't execute properly
ViewBag.Message = messages;
return View();
It will always stop on the execute call, throwing the general HResult E_FAIL COMException in .NET even though I know the error is a ZeroDivisionError in the script because I purposely put it in there (the code runs perfect in python and my .NET app if I fix the zero division error).
gp = new GeoProcessorclass()
beforetry
, and then callgp.getMessages()
inside thecatch
block. (In fact, the only thing you want totry
is the call toExecute
)gp
out into the local scope...