函数调用自身,无法避免 StackOverflow 错误

发布于 2024-12-08 19:50:37 字数 800 浏览 0 评论 0原文

我知道这是糟糕的编程实践和冗余编码,但我很好奇。

我只是在 while 循环中计算直到 9999999 的数字。 Add() 函数只是增加该数字并将其打印出来。

Add() 函数内,我调用 Add() 函数。

在出现 StackOverflow 异常之前,我的程序已成功打印出 12000 左右的数字。

所以我的问题=有没有办法忽略这个错误并继续计数?

我尝试过 On Error ResumeTry/Catchthrow

我的代码如下。

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    While number1 < 9999999
        Try
            add()
        Catch ex As StackOverflowException
            ''Do Stuff here
        End Try
    End While
End Sub


  Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1) ''Error is throw here
    add()
    Return number1
End Function

I understand that this is bad programming practice and redundant coding but I am curios.

I am simply counting a number up to 9999999 in a while loop. The Add() function simply increases that number and prints it out.

Inside the Add() Function I call the Add() function.

My program successfully prints out 12000ish numbers before I get a StackOverflow Exception.

So my question = Is there any way to ignore this error and keep counting?

I have tried On Error Resume, Try/Catch, and throw

My code is as follows.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    While number1 < 9999999
        Try
            add()
        Catch ex As StackOverflowException
            ''Do Stuff here
        End Try
    End While
End Sub


  Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1) ''Error is throw here
    add()
    Return number1
End Function

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挽袖吟 2024-12-15 19:50:37

有什么办法可以忽略这个错误

吗?没有。您唯一能做的就是从一开始就避免引起它。大多数时候,StackOverflowException 是无法捕获的。来自 MDSN

从 .NET Framework 2.0 版开始,StackOverflowException 对象无法被 try-catch 块捕获,并且默认情况下会终止相应的进程。

Is there any way to ignore this error

No. The only thing you can do is avoid causing it in the first place. Most of the time, StackOverflowExceptions are uncatchable. From MDSN:

Starting with the .NET Framework version 2.0, a StackOverflowException object cannot be caught by a try-catch block and the corresponding process is terminated by default.

比忠 2024-12-15 19:50:37

由于 VB.NET 不支持尾递归,因此您无法在不使用尾递归的情况下进行无限递归。最终导致堆栈溢出。

Since VB.NET does not support tail recursion, there is no way you can make an infinite recursion without eventually overflowing the stack.

夜无邪 2024-12-15 19:50:37

您永远不会从函数中返回:

Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1)
    add()
    Return number1
End Function

因此,add 调用add 调用add - 在某些时候您会破坏堆栈。您需要某种退出条件来测试。

例如:

Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1)

    If number1 < 99999999 Then
      add()
    Else
      Return number1
    End If
End Function

You never return from the function:

Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1)
    add()
    Return number1
End Function

So, add calls add calls add - at some point you are blowing the stack. You need some sort of exit condition to test for.

For example:

Function add()
    number1 = number1 + 1
    Trace.WriteLine(number1)

    If number1 < 99999999 Then
      add()
    Else
      Return number1
    End If
End Function
在梵高的星空下 2024-12-15 19:50:37

将您的方法从以下更改:

Function add()     
    number1 = number1 + 1     
    Trace.WriteLine(number1) ''Error is throw here     
    add()     
    Return number1 
End Function 

改为:

Function add()     
    number1 = number1 + 1     
    Trace.WriteLine(number1) ''Error is throw here     
    Return number1 
End Function 

保持代码的其余部分相同,并且不会发生堆栈溢出。

Change your method from this:

Function add()     
    number1 = number1 + 1     
    Trace.WriteLine(number1) ''Error is throw here     
    add()     
    Return number1 
End Function 

To this:

Function add()     
    number1 = number1 + 1     
    Trace.WriteLine(number1) ''Error is throw here     
    Return number1 
End Function 

Keep the rest of your code the same and the stack overflow will not happen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文