xref: /petsc/include/petscerror.h (revision 8da67ba8136e6e209c4c277eb7ab185dc2cd77c7)
154a8ef01SBarry Smith /*
2f621e05eSBarry Smith     Contains all error handling interfaces for PETSc.
354a8ef01SBarry Smith */
445d48df9SBarry Smith #if !defined(__PETSCERROR_H)
545d48df9SBarry Smith #define __PETSCERROR_H
6c22c1629SBarry Smith #include "petsc.h"
7e9fa29b7SSatish Balay PETSC_EXTERN_CXX_BEGIN
8c22c1629SBarry Smith 
954a8ef01SBarry Smith /*
1054a8ef01SBarry Smith    Defines the directory where the compiled source is located; used
115e97870eSBarry Smith    in printing error messages. Each makefile has an entry
125e97870eSBarry Smith    LOCDIR	  =  thedirectory
130cd5afcaSLois Curfman McInnes    and bmake/common_variables includes in CCPPFLAGS -D__SDIR__='"${LOCDIR}"'
14f621e05eSBarry Smith    which is a flag passed to the C/C++ compilers. This declaration below
15f621e05eSBarry Smith    is only needed if some code is compiled without the -D__SDIR__
1654a8ef01SBarry Smith */
17c22c1629SBarry Smith #if !defined(__SDIR__)
182ee1dbe0SBarry Smith #define __SDIR__ "unknowndirectory/"
1954a8ef01SBarry Smith #endif
2054a8ef01SBarry Smith 
2154a8ef01SBarry Smith /*
224f227f7cSBarry Smith    Defines the function where the compiled source is located; used
23f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
24f621e05eSBarry Smith    does not declare it.
254f227f7cSBarry Smith */
264a2ae208SSatish Balay #if !defined(__FUNCT__)
27da9b6338SBarry Smith #define __FUNCT__ "User provided function"
284f227f7cSBarry Smith #endif
294f227f7cSBarry Smith 
304f227f7cSBarry Smith /*
31329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
32e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
330e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
340f9cf654SBarry Smith      These are also define in include/finclude/petscerror.h any CHANGES here
350f9cf654SBarry Smith      must be also made there.
3645d48df9SBarry Smith 
3754a8ef01SBarry Smith */
382a6744ebSBarry Smith #define PETSC_ERR_MIN_VALUE        54   /* should always be one less then the smallest value */
392a6744ebSBarry Smith 
4045d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
4147794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
42e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
43e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
4445d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
45f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
46a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
47a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
48329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
49329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
50b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
511302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
5245d48df9SBarry Smith 
5345d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5445d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
55a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
5645d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
5745d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
584f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
594f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
606831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
61d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
62a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
634482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
64958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
65c2be2410SBarry Smith #define PETSC_ERR_ARG_DOMAIN       87   /* argument is not in domain of function */
664f227f7cSBarry Smith 
674f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
684f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
694f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
70a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
7145d48df9SBarry Smith 
72329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
739e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7454a8ef01SBarry Smith 
752a6744ebSBarry Smith #define PETSC_ERR_MAX_VALUE        88   /* this is always the one more than the largest error code */
762a6744ebSBarry Smith 
774b55f32eSBarry Smith #if defined(PETSC_USE_ERRORCHECKING)
7830de9b25SBarry Smith 
7930de9b25SBarry Smith /*MC
8030de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
8130de9b25SBarry Smith 
8230de9b25SBarry Smith    Not Collective
8330de9b25SBarry Smith 
8430de9b25SBarry Smith    Synopsis:
85a7cc72afSBarry Smith    void SETERRQ(PetscErrorCode errorcode,char *message)
8630de9b25SBarry Smith 
8730de9b25SBarry Smith 
8830de9b25SBarry Smith    Input Parameters:
8930de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
9030de9b25SBarry Smith -  message - error message
9130de9b25SBarry Smith 
9230de9b25SBarry Smith   Level: beginner
9330de9b25SBarry Smith 
9430de9b25SBarry Smith    Notes:
9530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
9630de9b25SBarry Smith 
9730de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9830de9b25SBarry Smith 
9930de9b25SBarry Smith 
10030de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
10130de9b25SBarry Smith 
10230de9b25SBarry Smith    Concepts: error^setting condition
10330de9b25SBarry Smith 
10491d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
10530de9b25SBarry Smith M*/
1064a2ae208SSatish Balay #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
10730de9b25SBarry Smith 
10830de9b25SBarry Smith /*MC
10930de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
11030de9b25SBarry Smith 
11130de9b25SBarry Smith    Not Collective
11230de9b25SBarry Smith 
11330de9b25SBarry Smith    Synopsis:
114a7cc72afSBarry Smith    void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
11530de9b25SBarry Smith 
11630de9b25SBarry Smith 
11730de9b25SBarry Smith    Input Parameters:
11830de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
11930de9b25SBarry Smith .  message - error message in the printf format
12030de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
12130de9b25SBarry Smith 
12230de9b25SBarry Smith   Level: beginner
12330de9b25SBarry Smith 
12430de9b25SBarry Smith    Notes:
12530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
12630de9b25SBarry Smith 
12730de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
12830de9b25SBarry Smith 
12930de9b25SBarry Smith    Concepts: error^setting condition
13030de9b25SBarry Smith 
13191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
13230de9b25SBarry Smith M*/
1334a2ae208SSatish Balay #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
13430de9b25SBarry Smith 
13530de9b25SBarry Smith /*MC
13630de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
13730de9b25SBarry Smith 
13830de9b25SBarry Smith    Not Collective
13930de9b25SBarry Smith 
14030de9b25SBarry Smith    Synopsis:
141a7cc72afSBarry Smith    void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
14230de9b25SBarry Smith 
14330de9b25SBarry Smith 
14430de9b25SBarry Smith    Input Parameters:
14530de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
14630de9b25SBarry Smith .  message - error message in the printf format
14730de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
14830de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
14930de9b25SBarry Smith 
15030de9b25SBarry Smith   Level: beginner
15130de9b25SBarry Smith 
15230de9b25SBarry Smith    Notes:
15330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
15430de9b25SBarry Smith 
15530de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
15630de9b25SBarry Smith 
15730de9b25SBarry Smith    Concepts: error^setting condition
15830de9b25SBarry Smith 
1596024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
16030de9b25SBarry Smith M*/
1614a2ae208SSatish Balay #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
16230de9b25SBarry Smith 
16330de9b25SBarry Smith /*MC
16430de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
16530de9b25SBarry Smith 
16630de9b25SBarry Smith    Not Collective
16730de9b25SBarry Smith 
16830de9b25SBarry Smith    Synopsis:
169a7cc72afSBarry Smith    void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
17030de9b25SBarry Smith 
17130de9b25SBarry Smith 
17230de9b25SBarry Smith    Input Parameters:
17330de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
17430de9b25SBarry Smith .  message - error message in the printf format
17530de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
17630de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
17730de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
17830de9b25SBarry Smith 
17930de9b25SBarry Smith   Level: beginner
18030de9b25SBarry Smith 
18130de9b25SBarry Smith    Notes:
18230de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
18330de9b25SBarry Smith 
184f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
185f621e05eSBarry Smith 
18630de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
18730de9b25SBarry Smith 
18830de9b25SBarry Smith    Concepts: error^setting condition
18930de9b25SBarry Smith 
1906024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
19130de9b25SBarry Smith M*/
1924a2ae208SSatish Balay #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
19330de9b25SBarry Smith 
1944a2ae208SSatish Balay #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
195a30c184eSMatthew Knepley #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
196a30c184eSMatthew Knepley #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6);}
197a30c184eSMatthew Knepley #define SETERRQ7(n,s,a1,a2,a3,a4,a5,a6,a7) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5,a6,a7);}
198e855a17bSBarry Smith #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
1999a00fa46SSatish Balay 
20030de9b25SBarry Smith /*MC
20130de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
20230de9b25SBarry Smith 
20330de9b25SBarry Smith    Not Collective
20430de9b25SBarry Smith 
20530de9b25SBarry Smith    Synopsis:
206a7cc72afSBarry Smith    void CHKERRQ(PetscErrorCode errorcode)
20730de9b25SBarry Smith 
20830de9b25SBarry Smith 
20930de9b25SBarry Smith    Input Parameters:
21030de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
21130de9b25SBarry Smith 
21230de9b25SBarry Smith   Level: beginner
21330de9b25SBarry Smith 
21430de9b25SBarry Smith    Notes:
21530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
21630de9b25SBarry Smith 
21730de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2185324ea47SKris Buschelman 
219fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2205324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2215324ea47SKris Buschelman 
2225324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2235324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2245324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
225fcecf507SKris Buschelman     a more appropriate construct for using PETSc Error Handling would be
2265324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
22730de9b25SBarry Smith 
22830de9b25SBarry Smith    Concepts: error^setting condition
22930de9b25SBarry Smith 
23091d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
23130de9b25SBarry Smith M*/
232c3be3f59SMatthew Knepley #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
23330de9b25SBarry Smith 
23407d81ca4SBarry Smith #define CHKERRV(n)             if (n) {n = PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");return;}
23594a56cdfSMatthew Knepley #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
23694a56cdfSMatthew Knepley #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
23785614651SBarry Smith 
238a9c3d0aeSMatthew Knepley #define CHKFPQ(f) if (f != f) {SETERRQ(PETSC_ERR_FP, "Invalid value: NaN");}
239a9c3d0aeSMatthew Knepley 
24030de9b25SBarry Smith /*MC
24130de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
24230de9b25SBarry Smith 
24330de9b25SBarry Smith    Not Collective
24430de9b25SBarry Smith 
24530de9b25SBarry Smith    Synopsis:
24691d3bdf4SKris Buschelman    CHKMEMQ;
24730de9b25SBarry Smith 
24830de9b25SBarry Smith   Level: beginner
24930de9b25SBarry Smith 
25030de9b25SBarry Smith    Notes:
251ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
25230de9b25SBarry Smith 
25330de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
25430de9b25SBarry Smith 
25530de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
25630de9b25SBarry Smith 
257f621e05eSBarry Smith     Use CHKMEMA for functions that return void
258f621e05eSBarry Smith 
25930de9b25SBarry Smith    Concepts: memory corruption
26030de9b25SBarry Smith 
2616024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
262ff002950SBarry Smith           PetscMallocValidate()
26330de9b25SBarry Smith M*/
264ff002950SBarry Smith #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
26585614651SBarry Smith 
266e68848bdSBarry Smith #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
267e68848bdSBarry Smith 
2689dd39edbSMatthew Knepley #if defined(PETSC_UNDERSCORE_CHKERR)
2696849ba73SBarry Smith extern  PetscErrorCode __gierr;
270f1af5d2fSBarry Smith #define _   __gierr =
271ac355199SBarry Smith #define ___  CHKERRQ(__gierr);
272f1af5d2fSBarry Smith #endif
273f1af5d2fSBarry Smith 
274d5e45103SBarry Smith #define               PETSC_EXCEPTIONS_MAX  256
275d5e45103SBarry Smith extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
276d5e45103SBarry Smith extern PetscInt       PetscErrorUncatchableCount;
277d5e45103SBarry Smith extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
278d5e45103SBarry Smith extern PetscInt       PetscExceptionsCount;
279d5e45103SBarry Smith 
2806024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode);
281d5e45103SBarry Smith EXTERN void PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode);
282d5e45103SBarry Smith 
283c132512eSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
28421dd7e22SBarry Smith EXTERN PetscTruth PETSC_DLLEXPORT PetscErrorIsCatchable(PetscErrorCode);
28575febeb1SBarry Smith /*MC
28621dd7e22SBarry Smith    PetscExceptionCaught - Indicates if a specific exception zierr was caught.
28775febeb1SBarry Smith 
28875febeb1SBarry Smith    Not Collective
28975febeb1SBarry Smith 
29075febeb1SBarry Smith    Synopsis:
29175febeb1SBarry Smith      PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
29275febeb1SBarry Smith 
29375febeb1SBarry Smith   Input Parameters:
29475febeb1SBarry Smith   + xierr - error code returned from PetscExceptionTry1()
29575febeb1SBarry Smith   - zierr - error code you want it to be
29675febeb1SBarry Smith 
29775febeb1SBarry Smith   Level: advanced
29875febeb1SBarry Smith 
29975febeb1SBarry Smith    Notes:
30075febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
30175febeb1SBarry Smith 
30221dd7e22SBarry Smith     Use PetscExceptionValue() to see if the current error code is one that has been "tried"
30321dd7e22SBarry Smith 
30475febeb1SBarry Smith   Concepts: exceptions, exception hanlding
30575febeb1SBarry Smith 
30675febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
30775febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
30875febeb1SBarry Smith M*/
3092a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
310d5e45103SBarry Smith   PetscInt i;
311d5e45103SBarry Smith   if (xierr != zierr) return PETSC_FALSE;
312d5e45103SBarry Smith   for (i=0; i<PetscErrorUncatchableCount; i++) {
313d5e45103SBarry Smith     if (PetscErrorUncatchable[i] == zierr) {
314d5e45103SBarry Smith       return PETSC_FALSE;
315d5e45103SBarry Smith     }
316d5e45103SBarry Smith   }
317d5e45103SBarry Smith   return PETSC_TRUE;
318d5e45103SBarry Smith }
319d5e45103SBarry Smith 
32075febeb1SBarry Smith /*MC
32175febeb1SBarry Smith    PetscExceptionValue - Indicates if the error code is one that is currently being tried
322d5e45103SBarry Smith 
32375febeb1SBarry Smith    Not Collective
32475febeb1SBarry Smith 
32575febeb1SBarry Smith    Synopsis:
32675febeb1SBarry Smith      PetscTruth PetscExceptionValue(PetscErrorCode xierr);
32775febeb1SBarry Smith 
32875febeb1SBarry Smith   Input Parameters:
32975febeb1SBarry Smith   . xierr - error code
33075febeb1SBarry Smith 
33175febeb1SBarry Smith   Level: developer
33275febeb1SBarry Smith 
33375febeb1SBarry Smith    Notes:
33475febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
33575febeb1SBarry Smith 
33621dd7e22SBarry Smith     Use PetscExceptionCaught() to see if the current error code is EXACTLY the one you want
33721dd7e22SBarry Smith 
33875febeb1SBarry Smith   Concepts: exceptions, exception hanlding
33975febeb1SBarry Smith 
34075febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
34175febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
34275febeb1SBarry Smith M*/
3432a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
344d5e45103SBarry Smith   PetscInt i;
345d5e45103SBarry Smith   for (i=0; i<PetscExceptionsCount; i++) {
346d5e45103SBarry Smith     if (PetscExceptions[i] == zierr) {
347d5e45103SBarry Smith       return PETSC_TRUE;
348d5e45103SBarry Smith     }
349d5e45103SBarry Smith   }
350d5e45103SBarry Smith   return PETSC_FALSE;
351d5e45103SBarry Smith }
3526024bd2cSBarry Smith 
3536024bd2cSBarry Smith /*MC
3546024bd2cSBarry Smith    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
3556024bd2cSBarry Smith          rather than an error. That is if that error code is treated the program returns to this level,
3566024bd2cSBarry Smith          but does not call the error handlers
3576024bd2cSBarry Smith 
3586024bd2cSBarry Smith    Not Collective
3596024bd2cSBarry Smith 
3606024bd2cSBarry Smith    Synopsis:
3616024bd2cSBarry Smith      PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
3626024bd2cSBarry Smith 
3636024bd2cSBarry Smith   Level: advanced
3646024bd2cSBarry Smith 
3656024bd2cSBarry Smith    Notes:
3666024bd2cSBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
3676024bd2cSBarry Smith 
36875febeb1SBarry Smith   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
36975febeb1SBarry Smith         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
37075febeb1SBarry Smith         of how the local try is ignored if a higher (in the stack) one is also in effect.
3716024bd2cSBarry Smith 
3726024bd2cSBarry Smith   Concepts: exceptions, exception hanlding
3736024bd2cSBarry Smith 
3746024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
37521dd7e22SBarry Smith           CHKERRQ(), PetscExceptionCaught(), PetscExceptionPush(), PetscExceptionPop()
3766024bd2cSBarry Smith M*/
3776024bd2cSBarry Smith extern PetscErrorCode PetscExceptionTmp;
378d5e45103SBarry Smith #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTmp)
3796024bd2cSBarry Smith 
38054a8ef01SBarry Smith #else
381f621e05eSBarry Smith 
382f621e05eSBarry Smith /*
383f621e05eSBarry Smith     These are defined to be empty for when error checking is turned off, with config/configure.py --with-errorchecking=0
384f621e05eSBarry Smith */
385f621e05eSBarry Smith 
38690d37a7cSBarry Smith #define SETERRQ(n,s) ;
38790d37a7cSBarry Smith #define SETERRQ1(n,s,a1) ;
38890d37a7cSBarry Smith #define SETERRQ2(n,s,a1,a2) ;
38990d37a7cSBarry Smith #define SETERRQ3(n,s,a1,a2,a3) ;
39090d37a7cSBarry Smith #define SETERRQ4(n,s,a1,a2,a3,a4) ;
3919bd8d7fdSSatish Balay #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
392742f4fedSDinesh Kaushik #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
3931ee4faa0SMatthew Knepley #define SETERRABORT(comm,n,s) ;
39485614651SBarry Smith 
3954f227f7cSBarry Smith #define CHKERRQ(n)     ;
3961ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3971ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
398*8da67ba8SSatish Balay #define CHKFPQ(f) ;
39985614651SBarry Smith #define CHKMEMQ        ;
40085614651SBarry Smith 
401f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
402f1af5d2fSBarry Smith #define _
403f1af5d2fSBarry Smith #define ___
404f1af5d2fSBarry Smith #endif
405f1af5d2fSBarry Smith 
406*8da67ba8SSatish Balay #define PetscExceptionPush(a)       0
407*8da67ba8SSatish Balay #define PetscExceptionPop(a)
408c132512eSBarry Smith #define PetscErrorSetCatchable(a,b) 0
409*8da67ba8SSatish Balay #define PetscErrorIsCatchable(a)    PETSC_FALSE
410*8da67ba8SSatish Balay 
411c132512eSBarry Smith #define PetscExceptionCaught(a,b)   PETSC_FALSE
412c132512eSBarry Smith #define PetscExceptionValue(a)      PETSC_FALSE
4136024bd2cSBarry Smith #define PetscExceptionTry1(a,b)     a
41454a8ef01SBarry Smith #endif
41554a8ef01SBarry Smith 
416ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void);
417ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **);
4186024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4196024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4206024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
421e8fb0fc0SBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscMPIAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4226024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4236024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
424e93bc3c1Svictor EXTERN PetscErrorCode PETSC_DLLEXPORT PetscReturnErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4256024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
4266024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
427ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void);
428ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*);
429ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
430ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void);
431329f5518SBarry Smith 
432329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
433ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscSetFPTrap(PetscFPTrap);
43454a8ef01SBarry Smith 
4353a40ed3dSBarry Smith /*
4363a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4373a40ed3dSBarry Smith */
43851d3a450SBarry Smith #if defined(PETSC_USE_DEBUG)
4393a40ed3dSBarry Smith 
440184914b5SBarry Smith #define PETSCSTACKSIZE 15
441184914b5SBarry Smith 
4423a40ed3dSBarry Smith typedef struct  {
4430e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
4440e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
4450e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
446184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
447184914b5SBarry Smith         int currentsize;
4483a40ed3dSBarry Smith } PetscStack;
4493a40ed3dSBarry Smith 
450ff73aad6SKris Buschelman extern PETSC_DLLEXPORT PetscStack *petscstack;
451ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCopy(PetscStack*,PetscStack*);
452ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPrint(PetscStack*,FILE* fp);
453184914b5SBarry Smith 
454184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
4553a40ed3dSBarry Smith 
4566d385327SIbrahima Ba 
45730de9b25SBarry Smith /*MC
45830de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
45930de9b25SBarry Smith         used for error handling.
46030de9b25SBarry Smith 
46130de9b25SBarry Smith    Synopsis:
46230de9b25SBarry Smith    void PetscFunctionBegin;
46330de9b25SBarry Smith 
46430de9b25SBarry Smith    Usage:
46530de9b25SBarry Smith .vb
46630de9b25SBarry Smith      int something;
46730de9b25SBarry Smith 
46830de9b25SBarry Smith      PetscFunctionBegin;
46930de9b25SBarry Smith .ve
47030de9b25SBarry Smith 
47130de9b25SBarry Smith    Notes:
47230de9b25SBarry Smith      Not available in Fortran
47330de9b25SBarry Smith 
47430de9b25SBarry Smith    Level: developer
47530de9b25SBarry Smith 
47630de9b25SBarry Smith .seealso: PetscFunctionReturn()
47730de9b25SBarry Smith 
47830de9b25SBarry Smith .keywords: traceback, error handling
47930de9b25SBarry Smith M*/
4803a40ed3dSBarry Smith #define PetscFunctionBegin \
481beb17490SBarry Smith   {\
482184914b5SBarry Smith    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
4834a2ae208SSatish Balay     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
484184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = __FILE__; \
485184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = __SDIR__; \
486184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = __LINE__; \
487184914b5SBarry Smith     petscstack->currentsize++; \
4883a40ed3dSBarry Smith   }}
4893a40ed3dSBarry Smith 
4905cd90555SBarry Smith #define PetscStackPush(n) \
491184914b5SBarry Smith   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
492184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = n; \
493184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = "unknown"; \
494184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = "unknown"; \
495184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
496184914b5SBarry Smith     petscstack->currentsize++; \
4975cd90555SBarry Smith   }}
4983a40ed3dSBarry Smith 
499d64ed03dSBarry Smith #define PetscStackPop \
500184914b5SBarry Smith   {if (petscstack && petscstack->currentsize > 0) {     \
501184914b5SBarry Smith     petscstack->currentsize--; \
502184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
503184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
504184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
505184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
5065cd90555SBarry Smith   }};
507d64ed03dSBarry Smith 
50830de9b25SBarry Smith /*MC
50930de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
51030de9b25SBarry Smith         used for error handling. Replaces return()
51130de9b25SBarry Smith 
51230de9b25SBarry Smith    Synopsis:
51330de9b25SBarry Smith    void PetscFunctionReturn(0);
51430de9b25SBarry Smith 
51530de9b25SBarry Smith    Usage:
51630de9b25SBarry Smith .vb
51730de9b25SBarry Smith     ....
51830de9b25SBarry Smith      PetscFunctionReturn(0);
51930de9b25SBarry Smith    }
52030de9b25SBarry Smith .ve
52130de9b25SBarry Smith 
52230de9b25SBarry Smith    Notes:
52330de9b25SBarry Smith      Not available in Fortran
52430de9b25SBarry Smith 
52530de9b25SBarry Smith    Level: developer
52630de9b25SBarry Smith 
52730de9b25SBarry Smith .seealso: PetscFunctionBegin()
52830de9b25SBarry Smith 
52930de9b25SBarry Smith .keywords: traceback, error handling
53030de9b25SBarry Smith M*/
5315cd90555SBarry Smith #define PetscFunctionReturn(a) \
532beb17490SBarry Smith   {\
53306d1fe2cSBarry Smith   PetscStackPop; \
5345cd90555SBarry Smith   return(a);}
535d64ed03dSBarry Smith 
536ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
537ff94ddecSSatish Balay   {\
5381fceb228SKris Buschelman   PetscStackPop; \
5391fceb228SKris Buschelman   return;}
540ff94ddecSSatish Balay 
5416d385327SIbrahima Ba 
5426d385327SIbrahima Ba #else
5436d385327SIbrahima Ba 
5443a40ed3dSBarry Smith #define PetscFunctionBegin
5453a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
5465665465eSBarry Smith #define PetscFunctionReturnVoid() return
547d64ed03dSBarry Smith #define PetscStackPop
548d64ed03dSBarry Smith #define PetscStackPush(f)
549d64ed03dSBarry Smith #define PetscStackActive        0
5503a40ed3dSBarry Smith 
5513a40ed3dSBarry Smith #endif
5523a40ed3dSBarry Smith 
553ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCreate(void);
554ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackView(PetscViewer);
555ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDestroy(void);
556ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPublish(void);
557ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDepublish(void);
5583a40ed3dSBarry Smith 
55906d1fe2cSBarry Smith 
560e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
56106d1fe2cSBarry Smith #endif
562