PETSc version 3.17.0
Fix/Edit manual page

# PetscCallCXX

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

### Synopsis

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

Not Collective

### Input Parameter

| expr | - An arbitrary expression

### Notes

PetscCallCXX(expr) is a macro replacement for

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

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

### 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 mutliple 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

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

### Level

beginner

### Location

src/sys/error/../../../include/petscerror.h
Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages