I think the solutions will work, but I have a similar case, where I think this solutions produce quite ugly/bad maintainable code.
In my case. I want to call a logout-method, whenever an exception occurs and than rethrow the occured exception.
try
{
...
}
catch (Exception e)
{
logout();
throw e;
}
This changes the type of exceptions surrounding method throws to 'Exception', which is not as specific as before. [changes of the 'throw'-list of the method]
The other solution:
try
{
...
}
catch (Foo1Exception e)
{
logout();
throw e;
}
catch (Foo2Exception e)
{
logout();
throw e;
}
...
In this version I have to build a indentic catch-block for all Exception-subtypes the surrounding method may throw.
-> redundant code
-> each time I add a new exception-sub-type to the method, I have to add an extra catch-block. :-(
Exists a better way?