I have the following function in my C# helper library which updates an object in a third-part application (CATIA V5):
public void Update(INFITF.AnyObject objectToUpdate)
{
try
{
Part.UpdateObject(objectToUpdate);
}
catch
{
throw new InvalidOperationException("Update Failed");
}
finally
{
Part.Inactivate(objectToUpdate);
}
}
It is unavoidable that Part.UpdateObject()
will sometimes fail.
In the case that this fails, I would like to notify the function user that the update failed, give them the opportunity to fix the problem in a custom way and handle the exception, or handle the exception in a default, stable way.
I know that the code I posted won't work because finally
blocks don't even get called if an exception is unhandled...but hopefully the code block gets the idea across.
UPDATE:
I haven't explained my scenario well enough so let me clarify. I am writing this function which other people will use as they develop. Some of these programmers know how to use try/catch blocks and some don't. Here are two sample scenarios where the function could be called.
A. the user is familiar with try/catch blocks and is aware of why the update call could fail.
try
{
Update(objectToUpdate);
}
catch (InvalidOperationException ex)
{
//fix the problem with the object to update
//I have handled the exception, so I DO NOT want the object to be inactivated
}
B. the user is not familiar with try/catch blocks or is not aware the update could fail
Update(objectToUpdate)
In this scenario we want to handle a failed update in a stable way that makes sense to a novice programmer and prevents the application from crashing. (in this case, inactivating the offending object in the third-party CAD software)