C# try catch finally Run code only if exception is unhandled
<p>I have the following function in my C# helper library which updates an object in a third-part application (CATIA V5):</p>

<pre><code>    public void Update(INFITF.AnyObject objectToUpdate)
    {
        try
        {
            Part.UpdateObject(objectToUpdate);
        }
        catch
        {
            throw new InvalidOperationException("Update Failed");
        }
        finally
        {
            Part.Inactivate(objectToUpdate);
        }
    }
</code></pre>

<p>It is unavoidable that <code>Part.UpdateObject()</code> will sometimes fail.</p>

<p>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. </p>

<p>I know that the code I posted won't work because <code>finally</code> blocks don't even get called if an exception is unhandled...but hopefully the code block gets the idea across. </p>

<p>UPDATE: </p>

<p>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. </p>

<p>A. the user is familiar with try/catch blocks and is aware of why the update call could fail. </p>

<pre><code>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
}
</code></pre>

<p>B. the user is not familiar with try/catch blocks or is not aware the update could fail</p>

<pre><code>Update(objectToUpdate)
</code></pre>

<p>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)</p>