Exceptions

Does anyone have any recommended best practices for handling exceptions in code that calls NX/Open and/or SNAP functions? Some specific questions are:
(1) What sort of code needs to be enclosed in a Try block. Everything?
(2) How do you know which exceptions might possibly be thrown? Just trial and error?
(3) What recovery code should go in the Catch block? Is UndoToMark the best approach?

Probably the best answer that I can give in this stage of my journey is: validation and testing, testing, testing. As we run into errors we file it away as "experience" and we are better equipped to handle similar situations in the future.

Your questions #1 and #3 are difficult to answer, they need to be assessed on a case by case basis.

As I'm writing code for a particular task, I'll perform the task a few times with interactive NX to better understand what the command needs and what problems may arise. In the code, I'll try to head off any potential problems. If I'm creating a block, for instance, I know that NX won't accept input for the edge lengths that is less than or equal to zero. I've never had a problem creating a block as long as the input was valid; therefore, I validate the input rather than catching an error with the block creation.

NX is a large, complex program with lots of opportunity for exceptions to arise. An argument could be made for even a seemingly small, simple journal to have several Try blocks. However, I'd caution against using Try blocks for everything. Avoid the errors that you can by validating the inputs, catch the errors that you find in testing.

As for what code goes in the Catch block, again it depends on what the code is doing, but the "undo to mark" is a good general strategy. If the code is applying an edge blend and you get the error "blend radius too small", you might try increasing the size in the Catch block. If you try to save a file and get an error "no write permission", the Catch block might fall back to writing to the user's temp directory.

There is a thread in the old GTAC forum about exception handling; unfortunately it didn't generate much discussion.
http://community.plm.automation.siemens.com/t5/NX-Languages/Does-your-co...

I wish I had more definitive answers. Exception handling seems to be a bit of a "black art". Hopefully others will add some tips and tricks that we can all learn from.