📄 group__g__error.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"><title>OCILIB (C Driver for Oracle): Error handling</title><link href="doxygen.css" rel="stylesheet" type="text/css"><link href="tabs.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.5.4 --><div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="modules.html"><span>Modules</span></a></li> <li><a href="annotated.html"><span>Data Structures</span></a></li> <li><a href="files.html"><span>Files</span></a></li> </ul></div><h1>Error handling</h1><hr><a name="_details"></a><h2>Detailed Description</h2>OCILIB provides two mechanisms for error handling:<p><ul><li>Global error handling through callbacks.</li><li>Contextual thread error handling</li></ul><p>Exceptions are raised:<p><ul><li>On Oracle OCI API call error</li><li>On Oracle SQL statement error</li><li>On Internal OCILIB error (type checking, memory allocations, ...)</li></ul><p>If an error handler was provided to <a class="el" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize()</a>, when an error occurs, the library generates an <a class="el" href="struct_o_c_i___error.html" title="Encapsulates an Oracle or OCILIB exception.">OCI_Error</a> handle and pass it to the error handler.<p>In order to use the thread contextual error handling, you must call <a class="el" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize()</a> with the flag OCI_ENV_CONTEXT for the mode parameter. When activated, error handles are stored per thread and the last error within a thread can be retrieved with <a class="el" href="group__g__error.html#gf2abe52ad5b278f65dd97a44a2adac4c" title="Retrieve the last error occurred within the last OCILIB call.">OCI_GetLastError()</a><p>Exception properties are accessible through a set of functions<p><dl class="note" compact><dt><b>Note:</b></dt><dd>The two ways to handle errors are not exclusive and can be mixed.<p>Thread contextual error is also available for single thread based applications</dd></dl><dl class="user" compact><dt><b>Example with callbacks</b></dt><dd><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "ocilib.h"</span><span class="keywordtype">void</span> err_handler(<a class="code" href="struct_o_c_i___error.html" title="Encapsulates an Oracle or OCILIB exception.">OCI_Error</a> *err){ printf( <span class="stringliteral">"code : ORA-%05i\n"</span> <span class="stringliteral">"msg : %s\n"</span> <span class="stringliteral">"sql : %s\n"</span>, <a class="code" href="group__g__error.html#g76fb68323de3574fb92a8027e8c4280e" title="Retrieve Oracle Error code from error handle.">OCI_ErrorGetOCICode</a>(err), <a class="code" href="group__g__error.html#ga06418c61c432aaaa725fecdd28bcb96" title="Retrieve error message from error handle.">OCI_ErrorGetString</a>(err), <a class="code" href="group__g__exec.html#g05c6c5af87cdff3a231755cfd44e133f" title="Return the last SQL or PL/SQL statement parsed by the statement.">OCI_GetSql</a>(<a class="code" href="group__g__error.html#gb099d59cd4faf2fa6a4430114ea73402" title="Retrieve statement handle within the error occured.">OCI_ErrorGetStatement</a>(err)) );}<span class="keywordtype">int</span> main(<span class="keywordtype">void</span>){ <a class="code" href="struct_o_c_i___connection.html" title="Oracle physical connection.">OCI_Connection</a> *cn; <span class="keywordflow">if</span> (!<a class="code" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize</a>(err_handler, NULL, OCI_ENV_DEFAULT)) <span class="keywordflow">return</span> EXIT_FAILURE; cn = <a class="code" href="group__g__connect.html#gdb6e2cdf759587dd81c2fda7c5f44338" title="Create a physical connection to an Oracle database server.">OCI_ConnectionCreate</a>(<span class="stringliteral">"wrong_db"</span>, <span class="stringliteral">"wrong_usr"</span>, <span class="stringliteral">"wrong_pwd"</span>, OCI_SESSION_DEFAULT); <span class="comment">/* ... application code here ... */</span> <a class="code" href="group__g__init.html#g639706aa8e9689c7ebffc018fac6d3ae" title="Clean up all resources allocated by the library.">OCI_Cleanup</a>(); <span class="keywordflow">return</span> EXIT_SUCCESS;}</pre></div></dd></dl><dl class="user" compact><dt><b>Example with thread context</b></dt><dd><div class="fragment"><pre class="fragment"><span class="preprocessor">#include "ocilib.h"</span><span class="keywordtype">int</span> main(<span class="keywordtype">void</span>){ <a class="code" href="struct_o_c_i___connection.html" title="Oracle physical connection.">OCI_Connection</a> *cn; <span class="keywordflow">if</span> (!<a class="code" href="group__g__init.html#gcdb642d75f7c8478e083634144bc813c" title="Initializes the library.">OCI_Initialize</a>(NULL, NULL, OCI_ENV_DEFAULT | OCI_ENV_CONTEXT)) <span class="keywordflow">return</span> EXIT_FAILURE; cn = <a class="code" href="group__g__connect.html#gdb6e2cdf759587dd81c2fda7c5f44338" title="Create a physical connection to an Oracle database server.">OCI_ConnectionCreate</a>(<span class="stringliteral">"wrong_db"</span>, <span class="stringliteral">"wrong_usr"</span>, <span class="stringliteral">"wrong_pwd"</span>, OCI_SESSION_DEFAULT); <span class="keywordflow">if</span> (cn == NULL) { <a class="code" href="struct_o_c_i___error.html" title="Encapsulates an Oracle or OCILIB exception.">OCI_Error</a> *err = <a class="code" href="group__g__error.html#gf2abe52ad5b278f65dd97a44a2adac4c" title="Retrieve the last error occurred within the last OCILIB call.">OCI_GetLastError</a>(); printf(<span class="stringliteral">"errcode %d, errmsg %s"</span>, <a class="code" href="group__g__error.html#g76fb68323de3574fb92a8027e8c4280e" title="Retrieve Oracle Error code from error handle.">OCI_ErrorGetOCICode</a>(err), <a class="code" href="group__g__error.html#ga06418c61c432aaaa725fecdd28bcb96" title="Retrieve error message from error handle.">OCI_ErrorGetString</a>(err)); } <a class="code" href="group__g__init.html#g639706aa8e9689c7ebffc018fac6d3ae" title="Clean up all resources allocated by the library.">OCI_Cleanup</a>(); <span class="keywordflow">return</span> EXIT_SUCCESS;}</pre></div> </dd></dl><p><table border="0" cellpadding="0" cellspacing="0"><tr><td></td></tr><tr><td colspan="2"><br><h2>Functions</h2></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT <a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#gf2abe52ad5b278f65dd97a44a2adac4c">OCI_GetLastError</a> (void)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve the last error occurred within the last OCILIB call. <a href="#gf2abe52ad5b278f65dd97a44a2adac4c"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT const mtext *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#ga06418c61c432aaaa725fecdd28bcb96">OCI_ErrorGetString</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve error message from error handle. <a href="#ga06418c61c432aaaa725fecdd28bcb96"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT unsigned int OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#g66d447c8cd06ea3f841b439a1aac055b">OCI_ErrorGetType</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve the type of error from error handle. <a href="#g66d447c8cd06ea3f841b439a1aac055b"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT int OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#g76fb68323de3574fb92a8027e8c4280e">OCI_ErrorGetOCICode</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve Oracle Error code from error handle. <a href="#g76fb68323de3574fb92a8027e8c4280e"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT int OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#ga59683a2cd8d8a5eb7d652fb153ff6c1">OCI_ErrorGetInternalCode</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve Internal Error code from error handle. <a href="#ga59683a2cd8d8a5eb7d652fb153ff6c1"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT <a class="el" href="struct_o_c_i___connection.html">OCI_Connection</a> *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#ga1dcd57ceeb422c050dd3791ff0372e1">OCI_ErrorGetConnection</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve connection handle within the error occurred. <a href="#ga1dcd57ceeb422c050dd3791ff0372e1"></a><br></td></tr><tr><td class="memItemLeft" nowrap align="right" valign="top">OCI_EXPORT <a class="el" href="struct_o_c_i___statement.html">OCI_Statement</a> *OCI_API </td><td class="memItemRight" valign="bottom"><a class="el" href="group__g__error.html#gb099d59cd4faf2fa6a4430114ea73402">OCI_ErrorGetStatement</a> (<a class="el" href="struct_o_c_i___error.html">OCI_Error</a> *err)</td></tr><tr><td class="mdescLeft"> </td><td class="mdescRight">Retrieve statement handle within the error occured. <a href="#gb099d59cd4faf2fa6a4430114ea73402"></a><br></td></tr></table><hr><h2>Function Documentation</h2><a class="anchor" name="ga1dcd57ceeb422c050dd3791ff0372e1"></a><!-- doxytag: member="ocilib.h::OCI_ErrorGetConnection" ref="ga1dcd57ceeb422c050dd3791ff0372e1" args="(OCI_Error *err)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT <a class="el" href="struct_o_c_i___connection.html">OCI_Connection</a>* OCI_API OCI_ErrorGetConnection </td> <td>(</td> <td class="paramtype"><a class="el" href="struct_o_c_i___error.html">OCI_Error</a> * </td> <td class="paramname"> <em>err</em> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Retrieve connection handle within the error occurred. <p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>err</em> </td><td>* error handle </td></tr> </table></dl><p>Definition at line <a class="el" href="error_8c-source.html#l00166">166</a> of file <a class="el" href="error_8c-source.html">error.c</a>.</p><p>References <a class="el" href="ocilib__types_8h-source.html#l00132">OCI_Error::con</a>.</p></div></div><p><a class="anchor" name="ga59683a2cd8d8a5eb7d652fb153ff6c1"></a><!-- doxytag: member="ocilib.h::OCI_ErrorGetInternalCode" ref="ga59683a2cd8d8a5eb7d652fb153ff6c1" args="(OCI_Error *err)" --><div class="memitem"><div class="memproto"> <table class="memname"> <tr> <td class="memname">OCI_EXPORT int OCI_API OCI_ErrorGetInternalCode </td> <td>(</td> <td class="paramtype"><a class="el" href="struct_o_c_i___error.html">OCI_Error</a> * </td> <td class="paramname"> <em>err</em> </td> <td> ) </td> <td width="100%"></td> </tr> </table></div><div class="memdoc"><p>Retrieve Internal Error code from error handle. <p><dl compact><dt><b>Parameters:</b></dt><dd> <table border="0" cellspacing="2" cellpadding="0"> <tr><td valign="top"></td><td valign="top"><em>err</em> </td><td>* error handle </td></tr> </table></dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -