Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

You want to handle fatal errors (assertions) yourself rather than have the library call abort().

Step-by-step guide

When there is an assertion failure, the library first checks whether the user has defined his/her own assertion handling mechanism. If not, it prints a message and calls abort. This is the default behaviour. But you can override this in the following way using the C API:

  1. Define a function with the following signature:

    Code Block
    languagecpp
    typedef void (*codes_assertion_failed_proc)(const char* message);


  2. Then callpoint the library to your function:

    Code Block
    languagecpp
    codes_set_codes_assertion_failed_proc(&your_function);


  3. Now the library will call the chosen function (which for example could do nothing or throw a C++ exception) instead of the abort.
  4. To restore the default behaviour, call

    Code Block
    languagecpp
    codes_set_codes_assertion_failed_proc(NULL);


...

Code Block
languagecpp
static void my_assertion_proc(const char* message)
{
    printf("It's OK. I caught the assertion: %s\n", message);
    // Do something else
}

 like throw MyProj::FatalException(message);
}

int main(int argc, char **argv)
{
    codes_set_codes_assertion_failed_proc(&my_assertion_proc);

    // DoAll lotsasserts ofin experimentalthe stuffecCodes knowinglibrary allwill asserts willnow be caught
    ...

    // Now restore default behaviour
    codes_set_codes_assertion_failed_proc(NULL);

    return 0;
}


Content by Label
showLabelsfalse
max5
spacesUDOC
showSpacefalse
sortmodified
reversetrue
typepage
excludeCurrenttrue
cqllabel in ("eccodes-faqs","kb-how-to-article") and label in ("assert","programming","bufr","grib") and type = "page" and space = "UDOC"
labelskb-how-to-article eccodes-faqs

...