From C++ Primer 18.1.1:
If the [thrown] expression has an array or function type, the expression is converted to its corresponding pointer type.
How is it that this program can produce correct output of 9876543210
(g++ 5.2.0)?
#include <iostream>
using namespace std;
int main(){
try{
int a[10] = {9,8,7,6,5,4,3,2,1,0};
throw a;
}
catch(int* b) { for(int i = 0; i < 10; ++i) cout << *(b+i); }
}
From the quote, throw a
would create an exception object of type int*
which is a pointer to the first element of the array. But surely the array elements of a
would be destroyed when we exit the try
block and enter the catch clause since we change block scope? Am I getting a false positive or are the array elements "left alone" (not deleted) during the duration of the catch clause?