PetscCallCXX — PETSc 3.25.1 documentation

Skip to main content
`Ctrl`+`K`

# PetscCallCXX

Checks C++ function calls and if they throw an exception, catch it and then return a PETSc error code

## Synopsis

    #include <petscsys.h>
    void PetscCallCXX(...) noexcept;

Not Collective

## Input Parameter

- VA_ARGS - An arbitrary expression

## Notes

`PetscCallCXX(...)` is a macro replacement for

      try {
        __VA_ARGS__;
      } catch (const std::exception& e) {
        return ConvertToPetscErrorCode(e);
      }

Due to the fact that it catches any (reasonable) exception, it is essentially noexcept.

If you cannot return a `PetscErrorCode` use `PetscCallCXXAbort()` instead.

## Example Usage

      void foo(void) { throw std::runtime_error("error"); }

      void bar()
      {
        PetscCallCXX(foo()); // ERROR bar() does not return PetscErrorCode
      }

      PetscErrorCode baz()
      {
        PetscCallCXX(foo()); // OK

        PetscCallCXX(
          bar();
          foo(); // OK multiple statements allowed
        );
      }

      struct bop
      {
        bop()
        {
          PetscCallCXX(foo()); // ERROR returns PetscErrorCode, cannot be used in constructors
        }
      };

      // ERROR contains do-while, cannot be used as function-try block
      PetscErrorCode qux() PetscCallCXX(
        bar();
        baz();
        foo();
        return 0;
      )

## See Also

`PetscCallCXXAbort()`, `PetscCallThrow()`, `SETERRQ()`, `PetscCall()`, `SETERRABORT()`, `PetscCallAbort()`, `PetscTraceBackErrorHandler()`, `PetscPushErrorHandler()`, `PetscError()`, `CHKMEMQ`

## Level

beginner

## Location

include/petscerror.h

Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages