OCILIB Tutorials v5.0.0
Beginnerdemo/err.cdemo/err_ctx.cdemo/err_warning.c

Error handling in depth

Objectives — by the end of this page you will be able to:

The error object

The error is different from every other handle in this tutorial: you never create or free one.

When a call fails, the library builds a small record describing exactly what went wrong and hands it to you through one of two channels — your error callback, or the thread's "last error" slot.

It's transient: read it, extract what you need, move on — the next failure replaces it.

In v5 every error answers three questions — how bad is it, who raised it, and which code identifies it — plus context about where it occurred.

Severity — the error's type:

C enum C++ enum Meaning
OCI_ERR_TYPE_ERROR Exception::Error failure — the call did not complete
OCI_ERR_TYPE_WARNING Exception::Warning non-fatal Oracle notice

Category — which layer reported it:

C enum C++ enum Meaning
OCI_ERR_CAT_LIB Exception::OcilibError OCILIB itself (null handle, bad arg, unsupported feature)
OCI_ERR_CAT_OCI Exception::OracleError the Oracle client/server (ORA-xxxxx)
OCI_ERR_CAT_JZN Exception::JsonError the JSON DOM engine (JZN-xxxxx)

Fields — everything the error object carries:

C function C++ method Returns
OCI_ErrorGetType GetType() severity enum above
OCI_ErrorGetCategory GetCategory() category enum above
OCI_ErrorGetCode GetCode() ORA-xxxxx or OCILIB internal code
OCI_ErrorGetString GetMessage() formatted message text
OCI_ErrorGetConnection GetConnection() handle involved (may be empty)
OCI_ErrorGetStatement GetStatement() statement involved (may be empty)
OCI_ErrorGetRow GetRow() batch/array row index when relevant
OCI_ErrorGetLocation GetLocation() the OCILIB function that raised it
— what() std::exception message (same text)

Two delivery channels

1. The error handler — the callback you registered when creating the environment. It fires immediately inside the failing call, before control returns to you; you can swap it at any time on the environment.

static void err_handler(OCI_Error *err)
{
    fprintf(stderr, "[%s/%s] %s in %s (code %u)\n",
        OCI_ErrorGetType(err)     == OCI_ERR_TYPE_WARNING ? "warn" : "err",
        OCI_ErrorGetCategory(err) == OCI_ERR_CAT_OCI      ? "oci"  : "lib",
        OCI_ErrorGetString(err), OCI_ErrorGetLocation(err),
        OCI_ErrorGetCode(err));
}

2. Polling the last error — every thread keeps a "last error" slot you can read after a failed call, handy when no handler is registered or you want details at the call site.

In C++, both channels collapse into one: a failed call throws ocilib::Exception carrying all the fields above.

if (!OCI_ConnectionCommit(cn))
{
    OCI_Error *err = OCI_GetLastError();
    fprintf(stderr, "commit failed: %s (code %u)\n",
            OCI_ErrorGetString(err), OCI_ErrorGetCode(err));
}

Warnings

By default only errors are reported. Enabling warnings on the environment routes Oracle notices (e.g. ORA-24347 on aggregation in a rollback) through the same handler, tagged with the warning severity so you can tell them apart:

OCI_EnvironmentEnableWarnings(env, TRUE);

C++ error handling

ocilib::Exception derives from std::exception — catch it specifically when you need the OCILIB fields (GetType, GetCategory, GetRow…); std::exception suffices otherwise.

There is no "last error" polling and no callback — the exception is the error object.

Putting it together — the typical sequence

Error handling is a contract, learned once and applied everywhere:

  1. Register the handler at EnvironmentCreate — void err_handler(OCI_Error *err) receives every failure.
  2. Check return codes — OCI_Boolean FALSE means failure; NULL means no-handle. That is the error signal in C, not an exception.
  3. Ask OCI_GetLastError for the details — OCI_ErrorGet* gives the message, ORA code, SQL statement, row.
  4. In C++ it's different: failures throw ocilib::Exception — the same core::Check wraps every call. try/catch, not return codes.

Hints: the handler is notification, not prevention — you still must check returns; GetLastError is per-thread — call it from the thread that failed; and the C++ exception carries the same OCI_Error fields — e.GetMessage(), e.GetOracleErrorCode() map one-to-one.

Try it yourself

  1. Force ORA-00942 (select * from nope) and print all error fields.
  2. Enable warnings, then run a query that produces one — compare its type to an error.
  3. In C, drop the handler (pass NULL at environment creation) and rely solely on last-error polling.

Common pitfalls

Recap

Next: watch every call — OCILIB's built-in logging subsystem.