gpt4 book ai didi

c# - Random.Next() 的异常使 try-catch block 中的程序崩溃

转载 作者:太空宇宙 更新时间:2023-11-03 23:22:49 25 4
gpt4 key购买 nike

所以,这发生了:

enter image description here

try block 中这怎么可能?为什么它不将其转发给 catch block ?

编辑:

有人指出,我可能有递归。我知道,我认为这不会造成问题。

完整的方法如下所示:

private static GeoCoordinate ChangeLocation(GeoCoordinate location)
{
var tmp = location;
var direction = new Random().Next(0, 359);
var distance = new Random().Next(0, 5);

//Calculate movement
var vertical = Math.Sin(direction) * distance; //Sinus relation shortened
var lastAngle = 180 - 90 - (direction % 90);
var horisontal = Math.Sin(lastAngle) * distance; //Sinus relation shortened

//Add movement to location
tmp.Latitude = location.Latitude + (vertical / 10000);
tmp.Longitude = location.Longitude + (horisontal / 10000);

//If new location is outside a specific area
if (!InsidePolygon(_siteCoordinates, tmp))
{
_recursiveCounter++;
//Ninja edit: @Leppie pointed out I was missing 'tmp =':
tmp = ChangeLocation(location); //Recursive move to calculate a new location
}

//Print the amount of recursive moves
if (_recursiveCounter != 0)
Console.WriteLine($"Counter: {_recursiveCounter}");
_recursiveCounter = 0;

return tmp;
}

最佳答案

Starting with 2.0 a StackOverflow Exception can only be caught in the following circumstances.

  1. The CLR is being run in a hosted environment where the host specifically allows for StackOverflow exceptions to be handled
  2. The stackoverflow exception is thrown by user code and not due to an actual stack overflow situation (Reference)

https://stackoverflow.com/a/1599238/4136669

MSDM

关于c# - Random.Next() 的异常使 try-catch block 中的程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34808110/

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