Revit -Add in exception handling

Revit -Add in exception handling

Anonymous
Not applicable
1,014 Views
4 Replies
Message 1 of 5

Revit -Add in exception handling

Anonymous
Not applicable

I have a semi-complicated Revit add-in which imports a simple text based file type. If the data in the file is incorrect it throws a nullreferenceexception, which I have wrapped in a try/catch block, this happens inside of a method and outside of a transaction. I've made it return a failed result and an error message to the user.

 

The problem is that the Add-in wont launch again after this, nothing happens when its clicked.

 

In another part of the Add-in program I have a WPF form which I use close() and dispose() on and I need something similar for this if anyone could send me some methods or guidance on best practice I could possibly investigate?

 

What do I need to call to "reset" the Add-in back to its original state?

 

I've searched through the building coders articles for an answer to this with no resolution so far.

 

Thanks all!

 

0 Likes
1,015 Views
4 Replies
Replies (4)
Message 2 of 5

RPTHOMAS108
Mentor
Mentor

You probably have to be a bit more specific and show some code, for example:

 

Are you using a modeless dialogue with an ExternalEvent? Are you still within the IExternalCommand context you implemented?

 

You should avoid using try/catch for a large block of code and instead anticipate the errors that could arise. If you are using a IO.StreamReader for example then you can check line by line for issues that could be wrong with the file you are importing.

 

There is usually no state that needs to be reset. If you are using ExternalEvent and the handler causes exception after exception you may find it stops working because a previously queued one is still stuck pending. Then you have to restart Revit, I've found this behaviour rarely.

0 Likes
Message 3 of 5

TripleM-Dev.net
Advisor
Advisor

Hi,

 

I don't think it's Revit API related....difficult to say without some code.

 


The problem is that the Add-in wont launch again after this, nothing happens when its clicked.


It won't launch after it throws a nullreferenceexception , or also if the data was correct?

Post the IExternalCommand.Execute method?

 

How is it called, does it hold a shared variable?

Maybe try it outside revit as standalone app. see if you can launch the file access part twice?

 

I always test UI outside Revit and my code/assemblies are also split in (minimal) 2 parts, one for all the Revit referenced code and one (minimal) for all the UI handling and general code for which no Revit reference is needed.

 

- Michel

 

0 Likes
Message 4 of 5

Anonymous
Not applicable

Thanks for the replies, I had too many interconnected files and methods to be able to post any code that would have been helpful.

 

I had to step through all of my code over and over and add some "try" & "Catch" loops, changed some methods to return some bool values to check the outputs along with resetting variables at different stages so needed a fair bit of refactoring.

0 Likes
Message 5 of 5

joshua.lumley
Advocate
Advocate

Try GC.Collect to perform a collection on all generations of memory. The code generates a number of unused objects, and then calls the Collect method to clean them from memory.

GC.Collect
0 Likes