TIBCO SmartSockets provides robust error handling for most API functions. The error handling system provides additional information about why a function call failed. This is common for functions returning TRUE
or FALSE
. For function calls that support error handling, the global TIBCO SmartSockets error code is set with a code that provides additional information about why the function failed. This global error code can be retrieved with TutErrNumGet. The Diagnostics section for each function in the TIBCO SmartSockets Application Programming Interface reference and the TIBCO SmartSockets Utilities reference describes how error handling is supported for that function. Error codes for the Tipc* API functions are included in the TIBCO SmartSockets Application Programming Interface reference. Reference pages on all the TutErr* functions are included in the TIBCO SmartSockets Utilities reference.
This example shows the proper way to write the example receiving program from Lesson 1 with error checking. Each of the calls to the TIBCO SmartSockets API is checked to see if an error occurred, and if so, an error message with its error code is output:
/* recv_err.c - read and print out an INFO message; error-checking included */
/* $RTHOME/examples/smrtsock/tutorial/lesson1/recv_err.c */
#include <rtworks/ipc.h> int main(int argc, char **argv) { T_IPC_MSG msg; T_STR msg_text; if (!TipcSrvSubjectSetSubscribe("/tutorial/lesson1", TRUE)) { TutOut("Can’t subscribe to </tutorial/lesson1>. Failed with error <%s>\n", TutErrStrGet()); TutExit(T_EXIT_FAILURE); } msg = TipcSrvMsgNext(T_TIMEOUT_FOREVER); if (msg == NULL) { TutOut("Could not retrieve message. Failed with error <%s>.\n", TutErrStrGet()); TutExit(T_EXIT_FAILURE); } if (!TipcMsgSetCurrent(msg, 0)) { TutOut("Can’t set message pointer. Failed with error <%s>.\n", TutErrStrGet()); TutExit(T_EXIT_FAILURE); } if (!TipcMsgNextStr(msg, &msg_text)) { TutOut("Can’t retrieve str field. Failed with error <%s>.\n", TutErrStrGet()); TutExit(T_EXIT_FAILURE); } TutOut("Text from INFO message = %s\n", msg_text); if (!TipcSrvDestroy(T_IPC_SRV_CONN_NONE)) { TutOut("Can’t disconnect. Failed with error <%s>.\n", TutErrStrGet()); TutExit(T_EXIT_FAILURE); } }/* main */
If you would like to run the example receiving program with error checking, copy the program recv_err.c
into your tutorial directory. Under Windows also copy the associated makefile, rcvew32m.mak
, into your tutorial directory. Compile and link the receiving program before running it:
Although adding error checking to your TIBCO SmartSockets applications makes them longer and a bit harder to read, it is highly recommended that you always check each call to the API for errors. This makes the debugging and testing of your distributed applications much easier.
To keep the examples simple, short, and easy to read, there is no error checking in any of the remaining examples in this tutorial. However, all published examples in the main set of TIBCO SmartSockets documentation include full error checking. In general, checking of error codes is a good practice to follow.
TIBCO SmartSockets™ Tutorial Software Release 6.8, July 2006 Copyright © TIBCO Software Inc. All rights reserved www.tibco.com |