Sre*_*ree 5 c++ arrays exception-handling
Normal函数(比如printArray)采用数组及其大小(2个参数)来打印数组的元素.
如何使用异常做同样的事情?更确切地说,如何传递数组大小以捕获处理程序?(假设我没有在try-catch之外声明的const int SIZE)例如.
//void printArray(int* foo ,int size);
int foo[] = { 16, 2, 77, 40, 12071 };
//printArray(foo,5); //OK, function call using array accepts size=5
try{
//do something
throw foo;
}
catch (int* pa)
{
//I have to get array size to loop through and print array values
// How to get array size?
}
Run Code Online (Sandbox Code Playgroud)
先感谢您
您可以通过以下方式将数组和它的大小作为一对抛出:
throw std::make_pair(foo, 5);
Run Code Online (Sandbox Code Playgroud)
并得到这样的两个值:
catch(std::pair<int*, int>& ex)
{
...
}
Run Code Online (Sandbox Code Playgroud)