PDA

View Full Version : My first TRY block




Maury
Aug 1, 2012, 12:25 PM
Consider this ODBC code...

nResult = SQLConnect(hdbc,
(UCHAR *)[dsn cStringUsingEncoding:defaultEncoding], SQL_NTS,
(UCHAR *)[userName cStringUsingEncoding:defaultEncoding], SQL_NTS,
(UCHAR *)[password cStringUsingEncoding:defaultEncoding], SQL_NTS);

// if failed to connect, free the allocated hdbc before return
if (nResult != SQL_SUCCESS && nResult != SQL_SUCCESS_WITH_INFO) {
[self logError:nResult forStatement:SQL_NULL_HSTMT];
SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
return NO;
}

The problem I'm having is that some ODBC drivers are poorly written, and SQLConnect simply dies with a BAD_EXEC.

I'd like to wrap this in a try block, which should flow through into the following IF.

Is the best solution to set a bool and read it in the IF, or is there a better style?



jared_kipe
Aug 1, 2012, 01:54 PM
I'd like to wrap this in a try block, which should flow through into the following IF.

Is the best solution to set a bool and read it in the IF, or is there a better style?

Unfortunately try will not help you in this example. BAD_EXEC is from something like dereferencing a null pointer and cannot be intercepted. (@try relies on exceptions being thrown, not just generic memory faults)

Are you sure that the BAD_EXEC isn't from something you've written?