包含在
尋找其他文件熱門支援資源 | 以 PDF 格式下載這本書 (911 KB)
Chapter 14 Handling ErrorsThe ToolTalk service returns error status in the function's return value rather than in a global variable. ToolTalk functions return one of these error values:
Each return type is handled differently to determine if an error occurred. For example, the return value for tt_default_session_set is a Tt_status code. If the ToolTalk service sets the default session to the specified sessid:
Retrieving ToolTalk Error StatusYou can use the ToolTalk error handling functions shown in Table 14–1 to retrieve error values. Table 14–1 Retrieving ToolTalk Error Status
The return type for these function is Tt_status. Checking ToolTalk Error StatusYou can use the ToolTalk error macro shown in Table 14–2 to check error values. Table 14–2 ToolTalk Error Macros
Returned Value StatusThe following sections describe the return value status of functions with natural return values and functions with no natural return value. Functions with Natural Return ValuesIf a ToolTalk function has a natural return value such as a pointer or an integer, a special error value is returned instead of the real value. Functions with No Natural Return ValuesIf a ToolTalk function does not have a natural return value, the return value is an element of Tt_status enum. To see if there is an error, use the ToolTalk macro tt_is_err, which returns an integer.
If there is an error, you can use the tt_status_message function to obtain the character string that explains the Tt_status code, as shown in Example 14–1. Example 14–1 Obtaining an Error Explanation
Returned Pointer StatusIf an error occurs during a ToolTalk function that returns a pointer, the ToolTalk service provides an address within the ToolTalk API library that indicates the appropriate Tt_status code. To check whether the pointer is valid, you can use the ToolTalk macro tt_ptr_error. If the pointer is an error value, you can use tt_status_message to get the Tt_status character string. Example 14–2 checks the pointer and retrieves and prints the Tt_status character string if an error value is found. Example 14–2 Retrieving a Returned Pointer Status
Returned Integer StatusIf an error occurs during a ToolTalk function that returns
an integer, the return value is out-of-bounds. The tt_int_error
function returns a status of To check if a value is out-of-bounds, you can use the tt_is_err macro to determine if an error or a warning occurred. To retrieve the character string for a Tt_status code, you can use tt_status_message. Example 14–3 checks a returned integer. Example 14–3 Checking a Returned Integer
Broken ConnectionsThe ToolTalk service provides a function to notify processes if your tool exits unexpectedly. When you include the tt_message_send_on_exit call, the ToolTalk service queues the message internally until one of two events happen:
Your process can also send a normal message on a normal termination by calling tt_message_send before it calls tt_close. In this case, if your process sends its normal termination message but crashes before it calls tt_close, the ToolTalk service will deliver both the normal termination message and the tt_message_send_on_exit message to interested processes. Error PropagationToolTalk functions that accept pointers always check the
pointer passed in and return In Example 14–4, a message is created, filled in, and sent. If tt_message_create fails, an error object is assigned to m, and all the tt_message_xxx_set and tt_message_send calls fail. To detect the error without checking between each call, you only need to check the return code from tt_message_send. Example 14–4 Error Checking
|
|||||||||||||||