View Full Version : catching API errors
Hi,
With my application I sometimes get an error which stops the API command running but doesn't crash Revit. Revit just returns as if the command ran correctly. And no error messages in the journals
Does anyone know if you can catch debugging messages when a command fails to run correctly? If you can do this, can you post an example?
TIA,
Guy
Danny Polkinhorn
2005-07-27, 08:51 AM
Guy,
It doesn't sound like you've been developing in .NET, but if you are, you can set up .NET to build your app and launch Revit at the same time. This will allow you to use the debugging features in .NET to trap for your errors. In the project properties, go to Configuration Properties > Debugging, choose the "Start External Application" option, and browse to the Revit executable. There might be something similar in your IDE.
You could also build in some error functions that alert you at the end using the "Failed" return value and the message parameter.
Hope that helps,
jim.awe
2005-07-27, 06:29 PM
Do you have your code in a try/catch block? .NET throws exceptions, it doesn't return error codes. Here is a simple example of defining an external command and catching all exceptions (errors). Here, I don't try to distinguish what happened, I just print an error message and return. If you can anticipate certain errors, you can try to catch specific exceptions and take the appropriate action.
public class CmdSnoopDb : IExternalCommand
{
public IExternalCommand.Result
Execute(Autodesk.Revit.Application app, ref string msg, ElementSet elems)
{
try {
Snoop.CollectorExts.CollectorExt.m_app = app;
Snoop.Forms.Objects form = new Snoop.Forms.Objects(app.ActiveDocument);
form.ShowDialog();
}
catch (System.Exception e) { // we want to catch it so we can see the problem, otherwise it just silently bails out
MessageBox.Show(e.Message);
throw e;
}
return IExternalCommand.Result.Succeeded;
}
}
NOTE: no matter how hard I try to format the above code so its readable, the newreader wants to squish it all together. Sorry.
Jim Awe
Autodesk, Inc.
It doesn't sound like you've been developing in .NET, but if you are, you can set up .NET to build your app and launch Revit at the same time.
Danny, great tip!! I'm not using VS but will see if this is an option with SharpDevelop.
Do you have your code in a try/catch block? .NET throws exceptions, it doesn't return error codes.
Jim, thanks for the example it's helped a lot. It certainly showed me what my problem is. Now to work out how to fix it ;-)
Cheers,
Guy
vBulletin® v3.6.7, Copyright ©2000-2010, Jelsoft Enterprises Ltd.