Welcome to MSDN Blogs Sign in | Join | Help

jaredpar's WebLog

Code, rants and ramblings of a programmer.

Syndication

News

Now Reading

Expert F#

What's a better book to read when learning F#?

Essential WPF

Thus far the best book I've read on WPF. Gets right down to working with WPF and the goals/history.

Purely Functional Data Structures

Reading this book makes me feel like I'm back in college. It will really get your mind going and is best read with a whiteboard handy.

Blog Roll

Eric Lippert
Dustin Campbell
Jon Skeet
Coding Horror
Brian McNamara
Brian Bondy
Hub FS
Full List

When can you catch a StackOverflowException?

Answer: When you're the one who threw it. 

Starting with the CLR version 2.0, the policy for handling a StackOverflowException was changed.  User code can no longer handle the exception[1].  Instead the CLR will simply terminate the process. 

This is not 100% true though.  User code can still handle StackOverflowExceptions which are artificially thrown.  That is thrown by the user instead of resulting from an actual overflow of the stack.  This is in contradiction to the documentation but can be demonstrated with a quick and dirty sample program (see end of the post). 

This is a trivial point for sure.  Yet I feel the need to point it out because I recently saw a newsgroup conversation where someone posted sample exception logging code and happened to use a StackOverflowException in the sample.  Their sample explicitly threw the exception so it worked and they had good reason to suspect it worked in production as well.  I was equally amazed it worked at all. 

Please don't take this post as advocating that you should handle a StackOverflowException (you shouldn't).  This is merely an oddity I found interesting.  Personally I'd prefer it not be catch-able in any circumstance.

public static void CatchStackOverflow1() {
    try {
        throw new StackOverflowException();
    } catch (StackOverflowException ex) {
        // Executes and handles the exception.  User code continues
        Console.WriteLine(ex.Message);
    }
}

static int CreateRealOverflow(int p1) {
    return 42 + CreateRealOverflow(p1 + 1);
}

public static void CatchStackOverflow2() {
    try {
        CreateRealOverflow(42);
    } catch (StackOverflowException ex) {
        // Will not execute
        Console.WriteLine(ex.Message);
    }
}

static void Main(string[] args) {
    CatchStackOverflow1();
    CatchStackOverflow2();
}

[1] Unless you are hosting the CLR in which case you can implement some recovery mechanism.  This is certainly the exception though and not the rule [2]

[2] Sorry for the pun

Published Wednesday, October 22, 2008 8:00 AM by Jared Parsons

Filed under: , ,

Comments

No Comments

New Comments to this post are disabled
Page view tracker