Catching Exceptions

As Arnost mentioned in a comment to an earlier post, the methods Selection.PickObject and Select.PickObjects throw exceptions when the user cancels the pick operation. This most commonly happens when the user presses the ESC key. Speaking of the ESC, did you read this http://www.nytimes.com/2012/10/07/magazine/who-made-that-escape-key.html?

If you run this macro and push ESC instead of making a selection, Revit will throw the exception, your code will not catch the exception, and you will see this:

Reference r = uidoc.Selection.PickObject(ObjectType.Element);
TaskDialog.Show("Thanks!", "You selected " + doc.GetElement(r).Name);

exception

Much better is for your code to handle the error by using a try/catch block. For example, this code displays a TaskDialog when the exception is thrown and then caught.

try
{
    Reference r = uidoc.Selection.PickObject(ObjectType.Element);
    TaskDialog.Show("Thanks!", "You selected " + doc.GetElement(r).Name);
}
catch (Autodesk.Revit.Exceptions.OperationCanceledException exception)
{
    TaskDialog.Show("Oh No!", "You did not make a selection so PickObject threw an exception: " + exception.Message);
}

catch

Advertisements

4 thoughts on “Catching Exceptions

  1. Hi,
    Basically I have coded an add-in that moves walls. After the user moves walls they click on a door to exit the while loop. The user can run the operation continuously and if they hit the escape key when using the add-in as designed the command exits without an issue. The problem arises if the user does crazy moves that contorts the walls back onto themselves, revit wants to delete these now erroneous back-bending walls however if the user hits the escape key a fatal error will occur. Is there any way to resolve this by preventing the user from using the escape key / right mouse click etc?

    I use the following catch statements, straight from the textbook:

    }//end execute method

    //If the user right-clicks or presses Esc, handle the exception
    catch (Autodesk.Revit.Exceptions.OperationCanceledException)
    {
    return Result.Cancelled;
    }
    //Catch other errors
    catch (Exception ex)
    {
    message = ex.Message;
    return Result.Failed;
    }

      • Hi Harry,
        I went down the dark path of using automatic transactions to “cut corners,” I refactored the code then added using blocks with manual transactions and the Revit Api can now once again handle the exceptions. Now for something completely different, have you ever created any automation in a Revit doc, where you show continuous movement? I can only manage discrete jumps, I think I used utils.move method and each move had to be in a separate using block of a manual transaction. I used some finite state machine code I found to action each move. Are there any revit api limitations in doing this?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s