DataBlade API Programmer's Manual
Chapter 9: Event and Exception Handling
Home
Contents
Index
Master Index
New Book
Exception Handling
When an exception is raised in the database server or in a user-defined routine, any callbacks registered for the exception are invoked. In addition, in the server DataBlade API, a callback can declare that it has handled the exception and that the exception is not to be propagated.
A user callback routine halts propagation of an exception with
MI_CB_EXC_HANDLED
. The callback can substitute its own actions for any actions that would have been taken had the exception been propagated, or it can do nothing at all.
The following example illustrates exception handling. The user function,
myfunc()
, is a server function that registers a callback named
myhandler
and uses
mi_exec()
to send a statement string that causes a syntax error in the database server.
/*
** Raise Callback Example
*/
#include <mi.h>
void myfunc( void )
{
static MI_CALLBACK_STATUS MI_PROC_CALLBACK myhandler();
MI_CONNECTION * conn;
/* database server connection */
conn = mi_open( NULL, NULL, NULL );
/* register the handler */
(void) mi_register_callback( conn, MI_Exception, myhandler, NULL, NULL );
/* generate a syntax error which myhandler will
* consume.
*/
(void) mi_exec( conn, "syntax error;", 0);
}
The user callback
myhandler()
catches and claims to handle the exception.
static MI_CALLBACK_STATUS MI_PROC_CALLBACK
myhandler(type, conn, cb_data, user_data)
MI_EVENT_TYPE type;
MI_CONNECTION *conn;
void *cb_data;
void *user_data;
{
/* claim to have handled the exception */
return MI_CB_EXC_HANDLED;
}
This callback suppresses any actions that the database server might take, such as aborting the statement. If no user callback existed for this exception, or if the callback did not handle the exception, the database server would handle the exception, and the callback function would return a message. However,
myhandler()
prevents the database server from handling the exception, and the caller
myfunc()
simply returns.
DataBlade API Programmer's Manual
, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.