gpt4 book ai didi

vb.net - 函数调用自身,无法避免 StackOverflow 错误

转载 作者:行者123 更新时间:2023-12-01 22:19:17 25 4
gpt4 key购买 nike

我知道这是不好的编程习惯和冗余编码,但我很好奇。

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

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

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

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

我尝试过错误恢复Try/Catch抛出

我的代码如下。

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

最佳答案

Is there any way to ignore this error

没有。你唯一能做的就是从一开始就避免造成这种情况。大多数时候,StackOverflowException 是无法捕获的。来自 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.

关于vb.net - 函数调用自身,无法避免 StackOverflow 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7688684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com