Hi akosi,
Try-catch statements are used to "try" and execute a piece of code and then "catch" any exceptions which are generated if the code fails to execute correctly. This means that instead of your application crashing, you can display an error message or handle the exception appropriately. The basic premise works like this:
try
{
// Attempt to execute this code
}
catch
{
// catch and deal with exceptions here
}
Further information can be found HERE, in Microsoft's C# language reference. As for the example you are working through, you can add a try catch to your code as follows:
[Transaction(TransactionMode.Manual)]
class HelloWorld : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiApp = commandData.Application;
UIDocument uiDoc = uiApp.ActiveUIDocument;
Application app = uiApp.Application;
Document doc = uiDoc.Document;
try
{
// Display Hello world! in TaskDialog
TaskDialog.Show("Try Catch Example", "Hello world!");
}
catch (Exception ex)
{
// Code did not execute successfully, display exception error
TaskDialog.Show("Exception Caught", "Exception: " + ex);
// return Failed
return Result.Failed;
}
// Code executed successfully, return Succeeded
return Result.Succeeded;
}
}
Hope this helps.
Roppa