Philip Rieck

Phil in .net

StackOverflow exception handling -- ideas ?



I'll admit it, sometimes my code has bugs -- hopefully all caught before release, but always the first cut has bugs. Today I hit one that made me think about something I may have been doing wrong for some time.

It's all about StackOverflowException. When one of these is thrown, you must rethrow it, and let the CLR terminate your application. Why? Well, as I've learned (and the name suggests), your app is now in an unstable state. Besides the obvious problems of being out of stack space, the not-so-obvious hits you -- finally blocks require stack space! Yes, this means that none of your finally blocks have been / will be executed from the time the exception happened going forward.

So, in the way past, I used to catch System.Exception in code where I knew I could simply drop whatever operation I was doing and recover. Oops, bad Idea -- you can't recover from everything (perhaps more on that later).

Now, I put cleanup in the finally block, logging and problem handling in the catch block, and rethrow anything I don't know how to handle (if I catch it at all). I thought that was great, until today.

If I'm using some precious resource that must be released or brought to a known state, I used to put that in the finally block. But if something goofy happens (Murphy insists that it will) and a StackOverflowException is thrown, then my finally is not called! Yikes!

Do I need cleanup in two places (catch and finally? catch and end of try block? )? I can't centralize it into a method, as I can't call into any methods during a StackOverflowException. I suppose I could say "that probably won't happen" and ignore it, but on a machine that is never rebooted, it might happen. I can live with monitoring my app and restarting it if the CLR must terminate it, but I can't live with my app slowly bringing the server into an unstable state. Any thoughts?


P.S - any thoughts on how to log a StackOverflowException?

Comments

Roy Osherove said:

I think that if you get that exception, your app is toast anyway. All you can do is alert the user "Hey, I'm shutting dowm, send my log to tech support" and then shut down.
Then, Use some kind of service as a "watch-dog" to restart your application if the process is not up.
# May 30, 2003 11:55 AM

Philip Rieck said:

I'm okay with having a watchdog service restart... but you're right, it looks like there's really no way to clean up. <br><br>StackOverflowException is really just one of those conditions that means "crash".
# May 30, 2003 12:00 PM

Philip Canarsky said:

One way to clean up might be to wrap the resource needing releasing in a using block (assuming it implements IDisposable). However, I have never tested whether Dispose() is called when execution leaves the using block due to a StackOverflowException being thrown. But it is an idea...
# June 3, 2003 1:25 PM

Philip Canarsky said:

One way to clean up might be to wrap the resource needing releasing in a using block (assuming it implements IDisposable). However, I have never tested whether Dispose() is called when execution leaves the using block due to a StackOverflowException being thrown. But it is an idea...
# June 3, 2003 1:25 PM

north face outlet said:

Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said "You can hear the ocean if you put this to your ear." She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is completely off topic but I had to tell someone!

# November 7, 2012 4:54 PM

Pate said:

Good response in return of this query with real arguments and describing

everything on the topic of that.

# April 2, 2013 7:40 PM

Boyette said:

This is a topic which is close to my heart... Best wishes!

Where are your contact details though?

# April 10, 2013 2:55 AM

Canfield said:

I really like reading an article that will make people think.

Also, thanks for allowing me to comment!

# April 12, 2013 12:30 AM

Canfield said:

I seldom comment, but i did some searching and wound up here StackOverflow exception handling -- ideas ?

- Philip Rieck. And I do have a couple of questions for you if it's allright. Is it only me or does it look like some of the responses come across as if they are written by brain dead visitors? :-P And, if you are writing at additional online social sites, I would like to keep up with anything new you have to post. Could you make a list of every one of your community sites like your twitter feed, Facebook page or linkedin profile?

# April 12, 2013 8:54 PM

Peace said:

Fantastic beat ! I wish to apprentice while you amend

your site, how could i subscribe for a blog

web site? The account helped me a acceptable deal.

I had been a little bit acquainted of this your broadcast provided bright

clear concept

# April 12, 2013 11:01 PM

Ridgeway said:

We are a group of volunteers and starting a new scheme

in our community. Your site provided us with valuable information to work on.

You've done a formidable job and our entire community will be thankful to you.

# April 13, 2013 3:59 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)