左右的時間內,發生這種情況:異常崩潰程序的try-catch塊
這怎麼可能一個try
- 塊內? 它怎麼沒有轉發到catch
- 塊?
編輯:
已經指出的,我可能有遞歸。我這樣做,我認爲這不會導致問題。
完整的方法是這樣的:
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;
}
這只是因爲您處於調試模式。如果你實際上沒有附加任何調試器就運行exe(雙擊它),那麼捕獲將按預期工作。 –
在調試器中查看您的調用堆棧,看看有什麼溢出。你可能在某處偶然調用一個方法遞歸。 – Baldrick
可能'ChangeLocation'是罪魁禍首,而不是'隨機' – Ian