PETSc version 3.17.4
Fix/Edit manual page

# PetscCall

Checks error code returned from a PETSc function, if non-zero it calls the error handler and and returns from the current function.

### Synopsis

    #include <petscerror.h>
    void PetscCall(PetscErrorCode ierr)

Not Collective

### Input Parameter

| ierr | - nonzero error code, see the list of standard error codes in include/petscerror.h

### Notes

Once the error handler is called the calling function is then returned from with the given error code. Experienced users can set the error handler with PetscPushErrorHandler().

PetscCall(ierr) is fundamentally a macro replacement for

      if (ierr) return PetscError(...,ierr,...);

PetscCall() cannot be used in functions returning a datatype not convertible to PetscErrorCode. For example, PetscCall() may not be used in functions returning void, use PetscCallVoid() in this case.

### Fortran Notes

PetscCall() may be called from Fortran subroutines but CHKERRA() must be called from the Fortran main program.

### Example Usage

      PetscCall(PetscInitiailize(...)); // OK to call even when PETSc is not yet initialized!

      extern int foo(int);

      PetscCall(foo(1)); // OK if int is convertible to PetscErrorCode

      struct my_struct
      {
        void *data;
      } my_complex_type;

      struct my_struct bar(void)
      {
        PetscCall(foo(15)); // ERROR PetscErrorCode not convertible to struct my_struct!
      }

      PetscCall(bar()) // ERROR input not convertible to PetscErrorCode

### See Also

SETERRQ(), PetscCheck(), PetscAssert(), PetscTraceBackErrorHandler(),
PetscPushErrorHandler(), PetscError(), CHKMEMQ, CHKERRA()

### Level

beginner

### Location

src/sys/error/../../../include/petscerror.h

### Examples

src/sys/classes/viewer/tutorials/ex1.c.html
src/sys/classes/viewer/tutorials/ex2.c.html
src/sys/classes/random/tutorials/ex1.c.html
src/sys/classes/random/tutorials/ex2.c.html
src/sys/tutorials/ex1.c.html
src/sys/tutorials/ex2.c.html
src/sys/tutorials/ex3.c.html
src/sys/tutorials/ex4.c.html
src/sys/tutorials/ex5.c.html
src/sys/tutorials/ex6.c.html
src/sys/tutorials/ex16.c.html
Index of all Sys routines
Table of Contents for all manual pages
Index of all manual pages