What is the prefered way of throwing exceptions?

What is the prefered way of throwing exceptions?

GertjanvandenbroekVKAE
Advocate Advocate
928 Views
3 Replies
Message 1 of 4

What is the prefered way of throwing exceptions?

GertjanvandenbroekVKAE
Advocate
Advocate

Just for general 'cleanliness' and proper use of provided classes.

Are we supposed to use the Autodesk.Revit.Exceptions or the System.Exeptions?

How do you properly throw a Autodesk.Revit.Exception?

0 Likes
Accepted solutions (1)
929 Views
3 Replies
Replies (3)
Message 2 of 4

mhannonQ65N2
Collaborator
Collaborator

Interesting question.

 

I know at least some of the Autodesk exceptions have a FunctionId property, which reports the C++ file and line (and function, I think) where the error occurred. I guess these are provided to assist in interop and debugging. As such, I don't think you should throw Autodesk exceptions that have a FunctionId property.

 

In general, in Revit add-ins, I recommend that you don't let any exception go uncaught, which causes a generic error message box to be shown to the user. Instead, you should either handle the erroneous condition the exception indicates in code (e.g. FileNotFoundException -> try looking for a different file or asking the user to provide a different file) or displaying a useful message to the user that concisely explains what went wrong and what they should do (e.g. when the user tries to give a sheet a number that is already in use, tell them that's what happened and ask for a different name).

Message 3 of 4

jeremytammik
Autodesk
Autodesk
Accepted solution

I am not an expert in this area, and I have not heard of any guidance on it from the Revit development team.

 

My intuitive assumption would be: the Autodesk Revit exceptions are specifically designed for Revit's own use. 

 

All Revit add-ins are external .NET code living in a different memory space.

 

I would therefore assume that the proper thing to do in a Revit add-in is to use the standard .NET add-ins according to the standard .NET best practices:

 

https://duckduckgo.com/?q=.net+exception+handling+and+definition+best+practice

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

0 Likes
Message 4 of 4

GertjanvandenbroekVKAE
Advocate
Advocate

Hmm yeah.

That does seem to make the most sense. They're also the easiest to create

0 Likes