Obscure exception handling facts

| No Comments | No TrackBacks

(The second in what seems to be a day of obscure .NET facts)

While looking at some C++/CLI code in Reflector today at work, we encountered the try ... fault construct.

What’s a fault block?

It’s like a finally block, but it’s only entered in the event of an exception.

So it’s like a catch block, then?

Not exactly:

  • fault blocks don’t have filters like catch blocks do
  • At the end of a fault block the exception is implicitly rethrown

Supposedly the benefit of a fault block is performance: that throw; statement at the end of a catch block has the same overhead as any other throw statement, whereas finally and fault blocks incur no runtime overhead. The stack is not unwound when a fault block is entered: fault blocks won’t appear in the exception stack trace.

Where do fault blocks show up?

In the code the C++/CLI compiler emits in order to make sure that local variables have their destructors called in the event of an exception.

No TrackBacks

TrackBack URL: http://mt.partario.com/mt-tb.cgi/65

Leave a comment

About this Entry

This page contains a single entry by Tim Robinson published on July 28, 2009 8:22 PM.

Obscure IEnumerator facts was the previous entry in this blog.

Control flow graph v0.1: Tail recursion is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.