2 Replies - 738 Views - Last Post: 19 February 2013 - 02:32 PM Rate Topic: -----

#1 triangulator  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 03-February 13

Catch clause fails to 'catch' the exception

Posted 19 February 2013 - 06:52 AM

I get a stackoverflow error message regardless that I have the corresponding catch clause. My question is not why the exception happens or what in my code causes it, but rather why I get the error message even when I have the corresponding catch clause.
try
            {
                double n;
                n = Convert.ToDouble(numberbox.Text);
                if (n <= 0) n = 0.0001;
                if (numberbox.Enabled == true)
                { s = n; if (radgrad!=null) if (!radgrad.Checked) s*=Math.PI/180; }
                if (functions.cut(s, 4) != s)
                {
                    numberbox.Text = functions.cut(s, 4).ToString();//it is on this line that I get the exception
                    if (radgrad != null) if (!radgrad.Checked) numberbox.Text = functions.cut(180 * s / Math.PI, 4).ToString();
                }
            }
            catch (StackOverflowException)
            {//I don't get to know if this code is ever excecuted, since the error message freezes the program
                if (numberbox.Text == "")
                {
                    numberbox.Text = "1";
                    if (numberbox.Enabled == true)
                        s = 1;
                }
                else
                {
                    numberbox.Text = functions.cut(s, 4).ToString();
                    if (radgrad != null) if (!radgrad.Checked) numberbox.Text = functions.cut(180 * s / Math.PI, 4).ToString();
                }
            }
            catch (Exception)
            {
                if (numberbox.Text == "")
                {
                    numberbox.Text = "1";
                    if (numberbox.Enabled == true)
                        s = 1;
                }
                else
                {
                    numberbox.Text = functions.cut(s, 4).ToString();
                    if (radgrad != null) if (!radgrad.Checked) numberbox.Text = functions.cut(180 * s / Math.PI, 4).ToString();
                }
            }



When the exception happens, n is 0.0001 and s is like 100 000 times smaller than that.

Is This A Good Question/Topic? 0
  • +

Replies To: Catch clause fails to 'catch' the exception

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,861
  • Joined: 02-June 10

Re: Catch clause fails to 'catch' the exception

Posted 19 February 2013 - 07:05 AM

Try---catch can only get you so far.

But it can't overcome truly bad design.

Stack overflow is because you haven an infinite loop. bad logic here.

A calls A
B calls A
See how this never ends.

Using try/catch to get out of bad design especially stack overflow is like adding 20 airbags to your car so you don't have to actually work at driving and can think of all the collisions as 'acceptable'.

You have several other issues with this code including using GUI controls like they are variables, code duplicate (both of your catch statements are the same)

Quote

10
numberbox.Text = functions.cut(s, 4).ToString();//it is on this line that I get the exception

Then the stack overflow is happening in that function - and it is probably not be handled there.


Since both of your catch blocks are the same, there is no need to break it out. Just use the last generic one to catch all.
Was This Post Helpful? 3
  • +
  • -

#3 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Catch clause fails to 'catch' the exception

Posted 19 February 2013 - 02:32 PM

As has been suggested, you shouldn't be catching exceptions like StackOverflowException, NullReferenceException, IndexOutOfBoundsException etc, as they indicate a fundamental flaw in your code; meaning the solution is to fix your code, not catch the exceptions.

Just to address why you cannot catch the StackOverflowException...Since .NET 2.0, Microsoft has made it uncatchable (unless you threw it yourself) - see here. Whenever a StackOverflowException is thrown by the Localloc IL instruction, the process is just terminated, and your catch block won't run.

Think about it for a second, and imagine the sort of things that could happen if a StackOverflowException was thrown, and then caught, causing execution to drop into your catch block. You've just blown the stack, but you then proceed to call a load of different methods from your catch block, like functions.cut(s, 4), for example. Chances are, those method calls will demand some stack space for local variables, return addresses etc. See the problem? :)

Having said that, from Microsoft's point of view, it is possible to allow developers to recover from a stack overflow (as proved by .NET 1.0), but the significant extra code required to make this reliable wasn't deemed to be worth it in .NET 2.0 onwards.

This post has been edited by CodingSup3rnatur@l-360: 19 February 2013 - 03:06 PM
Reason for edit:: Closed IL tag

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1