[英]try/catch missing vector subscript out of range (debug assertion failed)
I have some code, where I'm using std::forward_list
我有一些代码,在这里我使用std::forward_list
Part of my code tests for vector subscripts being out of range, as well as if the element in the vector has certain properties (in this case, is not -1). 我的代码部分测试了向量下标是否超出范围,以及向量中的元素是否具有某些属性(在这种情况下,不是-1)。
When I have just the first or second node of the list, it works fine and I can catch the error, as that's just a normal part of the program. 当我只有列表的第一个或第二个节点时,它可以正常工作,并且我可以捕获错误,因为那只是程序的正常部分。 However, on the next loop around, when I have 3 elements in the list, I get the vector subscript out of range
debug assertion, and it isn't caught by my try/catch
. 但是,在下一个循环中,当我在列表中有3个元素时,我得到了vector subscript out of range
调试断言,并且它不会被try/catch
。 Is there a different technique I must use to resolve this error? 我必须使用其他技术来解决此错误吗?
Here's a snippet of the code, where it's problematic: 这是一个有问题的代码段:
for(int i = 0; i < 8; i++)
{
if(curr->moves[i].at(0) >= 0 && curr->moves[i].at(1) >= 0)
{
next->setPos(curr->moves[i].at(0),curr->moves[i].at(1)); //just sets values of a vector
next->setMoves(); //sets values of a 2d vector
for(int j = 0; j < 8; j++)
{
try{if(board.kBoard[next->moves[i].at(0)][next->moves[i].at(1)] != -1) //the debug assertion is thrown here, but I cannot catch it
next->moves[i].at(2) = 1;}
catch(out_of_range const &err)
{next->moves[i].at(2) = 1;}
}
...
}
}
I've commented where the issue occurs. 我已评论发生问题的位置。 You'll (at least on my screen) have to scroll to the right to see it. 您(至少在我的屏幕上)必须滚动到右侧才能看到它。 I believe that, potentially, I can use another catch statement, but I don't know which error to use, given that I'm already using out_of_range
, which normally would handle this issue. 我相信,潜在地,我可以使用另一个catch语句,但是鉴于我已经在使用out_of_range
,通常我会处理该问题,所以我不知道要使用哪个错误。 A screenshot of what the error looks like when it's thrown is this . 引发错误时的屏幕快照是this 。 To be completely clear, the issue occurs at try{if(board.kBoard[next->moves[i].at(0)][next->moves[i].at(1) != -1)
, where I have commented. 要完全清楚,问题发生在try{if(board.kBoard[next->moves[i].at(0)][next->moves[i].at(1) != -1)
,其中我已经评论了。 It doesn't go to the catch
statement at all, I think because it's using _DEBUG_ERROR()
我认为它根本不会进入catch
语句,因为它使用的是_DEBUG_ERROR()
While, theoretically, a try/catch
statement could catch the error here, it isn't wise to intentionally cause errors as part of an algorithm; 从理论上讲,虽然try/catch
语句可以在此处捕获错误,但故意将错误作为算法的一部分来处理是不明智的; try/catch
should be used to report when errors happen. 应该使用try/catch
来报告何时发生错误。 In this case, I should have used: 在这种情况下,我应该使用:
if(next->moves[i].at(0) >= 0 && next->moves[i].at(1) >= 0) //1
if(board.kBoard[next->moves[i].at(0)][next->moves[i].at(1)] != -1) //2
next->moves[i].at(2) = 1; //3
else
next->moves[i].at(2) = 1; //3
This allows me to check if the subscript is out of range (1), check if an element of the board is not -1 (2), and set moves[i].at(2)
to 1, in those cases (3). 这使我可以检查下标是否超出范围(1),检查板的元素是否不为-1(2),然后在这些情况下将moves[i].at(2)
为1(3) )。
This avoids any out_of_range
errors, obviating the need for try/catch
entirely. 这避免了任何out_of_range
错误,从而完全避免了try/catch
的需要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.