I have a function in which I call getaddrinfo()to get sockaddr*, which is memory-oriented, allocated by the system. As many people know, you need to call freeaddrinfo()to free the memory allocated by getaddrinfo ().
Now, in my function, there are several places where I can throw an exception because some function failed. My first solution was to include freeaddrinfo()in every if-block. But it looked ugly to me, because I would have to call it anyway before returning my function, so I came up with an attempt to SEH-finally ...
But the problem I ran into is that it is not allowed to code throw expressions in __try-block
Then I read in msdn and tried to change the throw statements to a helper function called from the __try block ... and voila, the compiler no longer moaned ...
Why? And is it safe? It doesn't make sense to me: /
the code:
void function()
{
addrinfo* pFinal;
__try
{
getaddrinfo(..., &pFinal);
Helper();
}
__finally
{
freeaddrinfo();
}
}
void Helper()
{
throw(Exception);
}
EDIT:
tried the following and it works with throwing an integer, but not when I use the class as an exception:
class X
{
public:
X(){};
~X(){};
};
void Helper()
{
throw(X());
}
void base()
{
__try
{
std::cout << "entering __try\n";
Helper();
std::cout << "leaving __try\n";
}
__finally
{
std::cout << "in __finally\n";
}
};
int _tmain(int argc, _TCHAR* argv[])
{
try
{
base();
}
catch(int& X)
{
std::cout << "caught a X" << std::endl;
}
std::cin.get();
return 0;
}
Why?:/
source
share