I would like a review and recommendations for improvement of the code below. I want to allow an exception to be handled by the caller, so I am rethrowing it. But I have resources to close. So I have the resource closing code duplicated in both the try and the catch blocks. Is there a better way to do this?
const string HTTPopotamus::GET(void) {
HINTERNET hSession = NULL,
hConnect = NULL,
hRequest = NULL;
try {
hSession = this->open();
hConnect = this->connect(hSession);
hRequest = this->openRequest(hConnect);
this->sendRequest(hRequest);
this->receiveResponse(hRequest);
string strData = this->readData(hRequest);
/* Close any open resources. */
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
return strData;
} catch (const ExceptionCyclOps& e) {
/* Close any open resources. */
if( hRequest ) WinHttpCloseHandle( hRequest );
if( hConnect ) WinHttpCloseHandle( hConnect );
if( hSession ) WinHttpCloseHandle( hSession );
throw;
}
}