Getting error messages for python scripts run in .NET application

474
1
Jump to solution
11-08-2017 11:24 AM
MKF62
by
Occasional Contributor III

I feel like I'm missing something really obvious, but I can't figure this out. I use ArcObjects to call out to a python script using gp.execute(). When that python script encounters an error, the c# script will stop executing, leaving me no way to get error messages from the python script because anything like gp.GetMessage(ref sev) has to come after the python script call, so it's never hit when the script is erroneous. How do you get your error messages??

Example:

//junk here
gp.Execute("myscript", parameters, null); //error occurs here
gp.GetMessages(ref sev); //cannot actually get these messages because c# script stops executing

What about try/catch blocks?

try{
//junk here
gp.Execute("my script, parameters, null) //error occurs
}
catch{
gp.GetMessages(ref sev) //can't reference gp object here because it is not in scope
}
Tags (3)
0 Kudos
1 Solution

Accepted Solutions
MKF62
by
Occasional Contributor III

The solution is to move everything out of the "try" block except for the execute statement. That way the gp object is moved into the local scope where it can be accessed by the "catch" block. 

IGeoProcessor2 gp = new GeoProcessorClass();
gp.AddToolbox("myPathToToolboxPopulatePatches");
IVariantArray parameters = new VarArrayClass();
parameters.Add(shapefile);

try
{
   gp.Execute("PopulatePatches",parameters, null);
}
catch
{
   object erSeverity = 2;
   string erMessages = gp.GetMessages(ref erSeverity);
   ViewBag.Messages = erMessages;
   return View();
}

//more code if tool executes properly
return View();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

0 Kudos
1 Reply
MKF62
by
Occasional Contributor III

The solution is to move everything out of the "try" block except for the execute statement. That way the gp object is moved into the local scope where it can be accessed by the "catch" block. 

IGeoProcessor2 gp = new GeoProcessorClass();
gp.AddToolbox("myPathToToolboxPopulatePatches");
IVariantArray parameters = new VarArrayClass();
parameters.Add(shapefile);

try
{
   gp.Execute("PopulatePatches",parameters, null);
}
catch
{
   object erSeverity = 2;
   string erMessages = gp.GetMessages(ref erSeverity);
   ViewBag.Messages = erMessages;
   return View();
}

//more code if tool executes properly
return View();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos