Preserving the Stack Trace

Rethrowing Exceptions

Normally, if you have to rethrow and existing exception in .NET, you should use the throw keyword without any parameters:

try
{
    // Code that can generate exceptions here
}
catch (Exception ex)
{
    // Do some logging or whatever
    throw;
}

Using throw ex; would cause the stack trace of the exception to be lost, in which case we lose some valuable information. However, this only works inside the catch block that caught the exception in the first place.

Sometimes, you need to rethrow the exception at a later point outside the catch block, but of course we want to preserve the stack trace.

More...

Indisposable WCF clients

I have spent a considerable amount of time on development in a service-oriented architecture. On the Microsoft platform, the technology of choice is WCF (Windows Communication Foundation). Generally, it is a great technology, but it does have its nuisances. One of them is the way certain error conditions are handled when consuming web services.

More...