Example using catch

This proc closes everything wrapped in catch with added output if the routine does not return SQL_SUCCESS*.

######################################################################
#  Name:   CloseAfterError
#
#                clean up after an error in a specified part
#                of the database processing
#  Usage:  ODBC::ORACLE::CloseAfterError mode [hstmt]
#      where:
#          mode - operation being performed when error occurred
#          hstmt - statement handle (optional)  #
#  Returns: nothing
#  Notes:
#            In case of an error during the connect or later ODBC
#            operations, rewind things by doing the free statement handle,
#            disconnect, free connection handle dance.  Unset the connection 
#            handle namespace variable as the signal that the connection
#            handle should be reallocated, etc., when the next attempt is
#            made to perform a database operation.
#            If and error is encountered in allocating the environment or
#            connection handles, etc., it is probably unrecoverable. An error
#            message is emitted. The process is stopped.
proc CloseAfterError {} {
   variable hdbc
   variable henv
   variable hstmt
        if {$hstmt == {}} {
          puts stderr "ODBC Error - CloseAfterError called without an hstmt parameter set"
           return
        }
        if [ catch { odbc SQLCloseCursor $hstmt } errHstmt ] {
	        puts $errHstmt
        }
        set rVal [ catch { odbc SQLFreeStmt $hstmt SQL_DROP } errMsg ] {
	           puts stderr "ODBC Error - in SQLFreeStmt SQL_DROP"
}
        set rVal [ catch {odbc SQLFreeHandle SQL_HANDLE_STMT $hstmt} ]
        if {$rVal != "SQL_SUCCESS" && $rVal != "SQL_SUCCESS_WITH_INFO"} {
           puts stderr "ODBC Error - in SQLFreeHandle SQL_HANDLE_STMT"
        }
        set rVal [ catch {odbc SQLDisconnect $hdbc} ]
        if {$rVal != "SQL_SUCCESS" && $rVal != "SQL_SUCCESS_WITH_INFO"} {
            puts stderr "ODBC Error - in SQLDisconnect $hdbc"
        }
        set rVal [ catch { odbc SQLFreeHandle SQL_HANDLE_DBC $hdbc } ]
        if {$rVal != "SQL_SUCCESS" && $rVal != "SQL_SUCCESS_WITH_INFO"} {
            puts stderr "ODBC Error - in SQLFreeHandle SQL_HANDLE_DBC"
        }
        set hdbc {}
        return
   } ;# end close after error