HELLOWORLD WALKTROUGH LESSON - USING TRY CATCH - NEED SOME GUIDANCE

HELLOWORLD WALKTROUGH LESSON - USING TRY CATCH - NEED SOME GUIDANCE

Anonymous
Not applicable
1,128 Views
2 Replies
Message 1 of 3

HELLOWORLD WALKTROUGH LESSON - USING TRY CATCH - NEED SOME GUIDANCE

Anonymous
Not applicable

https://knowledge.autodesk.com/search-result/caas/CloudHelp/cloudhelp/2015/ENU/Revit-API/files/GUID-...

 

lessontrycatch.png

in this lesson, its talking about using try catch

but it doesnt tell me how or where to add this in the walkthrough helloworld sln

 

can somebody explain?

0 Likes
Accepted solutions (2)
1,129 Views
2 Replies
Replies (2)
Message 2 of 3

Anonymous
Not applicable
Accepted solution

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

 

 

0 Likes
Message 3 of 3

Anonymous
Not applicable
Accepted solution

Thank you for explaining!

0 Likes