C++ 异常模块测试C++;
我在测试方法中的异常时遇到问题。我发现C2337 ExpectedException:属性未找到时出错C++ 异常模块测试C++;,c++,unit-testing,exception,C++,Unit Testing,Exception,我在测试方法中的异常时遇到问题。我发现C2337 ExpectedException:属性未找到时出错 void Railcar::AddPlace(std::string name, unsigned int number) { if (number > PlaceVector.size()) throw std::exception("Place not found"); if (PlaceVector[number
void Railcar::AddPlace(std::string name, unsigned int number)
{
if (number > PlaceVector.size())
throw std::exception("Place not found");
if (PlaceVector[number - 1].GetNamePassenger() != " ")
throw std::exception("Place is occup");
PlaceVector[number - 1] = Place(number, name);
}
[ExpectedException("Place not found")]
TEST_METHOD(RailcarAddPlaceExseptionTest)
{
Railcar railcar(Luxury, 50, 1);
railcar.CreatePlase();
auto func = [&railcar] { railcar.AddPlace("John Titor", 51); };
Assert::ExpectException<int>(func);
}
void Railcar::AddPlace(std::字符串名称,无符号整数)
{
if(number>PlaceVector.size())
抛出std::异常(“未找到位置”);
if(PlaceVector[number-1].GetNamePassenger()!=“”)
抛出std::异常(“位置被占用”);
PlaceVector[编号-1]=地点(编号、名称);
}
[预期异常(“未找到位置”)]
试验方法(RailCaradPlaceExseption试验)
{
轨道车轨道车(豪华型,50,1);
railcar.CreatePlase();
auto func=[&railcar]{railcar.AddPlace(“John Titor”,51);};
Assert::ExpectException(func);
}
C++中的属性与C语言中的属性不同,因为您不能定义自定义属性。
这是一个可用的C++属性列表:
我是否能够检查异常的类型?[[noreturn]]获取任何异常,如果可能,我想将它们分开。@Gogogo您可以使用try…catch
即try{…}catch(const ArgumentException&ex){…}catch(const parametereexception&ex){…}