xref: /petsc/include/petscerror.h (revision e68848bdcd5ce88113591298ad148f80b60bb1d6)
154a8ef01SBarry Smith /*
24f227f7cSBarry Smith     Contains all error handling code 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}"'
145e97870eSBarry Smith    which is a flag passed to the C/C++ compilers.
1554a8ef01SBarry Smith */
16c22c1629SBarry Smith #if !defined(__SDIR__)
172ee1dbe0SBarry Smith #define __SDIR__ "unknowndirectory/"
1854a8ef01SBarry Smith #endif
1954a8ef01SBarry Smith 
2054a8ef01SBarry Smith /*
214f227f7cSBarry Smith    Defines the function where the compiled source is located; used
224f227f7cSBarry Smith    in printing error messages.
234f227f7cSBarry Smith */
244a2ae208SSatish Balay #if !defined(__FUNCT__)
25da9b6338SBarry Smith #define __FUNCT__ "User provided function"
264f227f7cSBarry Smith #endif
274f227f7cSBarry Smith 
284f227f7cSBarry Smith /*
29329ffe3dSLois Curfman McInnes      These are the generic error codes. These error codes are used
30e2d1d2b7SBarry Smith      many different places in the PETSc source code. The string versions are
310e5e90baSSatish Balay      at src/sys/error/err.c any changes here must also be made there
3245d48df9SBarry Smith 
3354a8ef01SBarry Smith */
3445d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
35106f7b34SBarry Smith #define PETSC_ERR_MEM_MALLOC_0     85   /* cannot malloc zero size */
3647794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
37e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
38e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
3945d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
40f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
41a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
42a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
43329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
44329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
45b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
461302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
4745d48df9SBarry Smith 
4845d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
4945d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
50a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
5145d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
5245d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
534f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
544f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
556831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
56d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
57a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
584482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
59958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
60c2be2410SBarry Smith #define PETSC_ERR_ARG_DOMAIN       87   /* argument is not in domain of function */
614f227f7cSBarry Smith 
624f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
634f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
644f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
65a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
6645d48df9SBarry Smith 
67329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
689e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
6954a8ef01SBarry Smith 
704b55f32eSBarry Smith #if defined(PETSC_USE_ERRORCHECKING)
7130de9b25SBarry Smith 
7230de9b25SBarry Smith /*MC
7330de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
7430de9b25SBarry Smith 
7530de9b25SBarry Smith    Not Collective
7630de9b25SBarry Smith 
7730de9b25SBarry Smith    Synopsis:
78a7cc72afSBarry Smith    void SETERRQ(PetscErrorCode errorcode,char *message)
7930de9b25SBarry Smith 
8030de9b25SBarry Smith 
8130de9b25SBarry Smith    Input Parameters:
8230de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
8330de9b25SBarry Smith -  message - error message
8430de9b25SBarry Smith 
8530de9b25SBarry Smith   Level: beginner
8630de9b25SBarry Smith 
8730de9b25SBarry Smith    Notes:
8830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
8930de9b25SBarry Smith 
9030de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9130de9b25SBarry Smith 
9230de9b25SBarry Smith 
9330de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
9430de9b25SBarry Smith 
9530de9b25SBarry Smith    Concepts: error^setting condition
9630de9b25SBarry Smith 
9791d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
9830de9b25SBarry Smith M*/
994a2ae208SSatish Balay #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
10030de9b25SBarry Smith 
10130de9b25SBarry Smith /*MC
10230de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
10330de9b25SBarry Smith 
10430de9b25SBarry Smith    Not Collective
10530de9b25SBarry Smith 
10630de9b25SBarry Smith    Synopsis:
107a7cc72afSBarry Smith    void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
10830de9b25SBarry Smith 
10930de9b25SBarry Smith 
11030de9b25SBarry Smith    Input Parameters:
11130de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
11230de9b25SBarry Smith .  message - error message in the printf format
11330de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
11430de9b25SBarry Smith 
11530de9b25SBarry Smith   Level: beginner
11630de9b25SBarry Smith 
11730de9b25SBarry Smith    Notes:
11830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
11930de9b25SBarry Smith 
12030de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
12130de9b25SBarry Smith 
12230de9b25SBarry Smith    Concepts: error^setting condition
12330de9b25SBarry Smith 
12491d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
12530de9b25SBarry Smith M*/
1264a2ae208SSatish Balay #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
12730de9b25SBarry Smith 
12830de9b25SBarry Smith /*MC
12930de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
13030de9b25SBarry Smith 
13130de9b25SBarry Smith    Not Collective
13230de9b25SBarry Smith 
13330de9b25SBarry Smith    Synopsis:
134a7cc72afSBarry Smith    void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
13530de9b25SBarry Smith 
13630de9b25SBarry Smith 
13730de9b25SBarry Smith    Input Parameters:
13830de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
13930de9b25SBarry Smith .  message - error message in the printf format
14030de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
14130de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
14230de9b25SBarry Smith 
14330de9b25SBarry Smith   Level: beginner
14430de9b25SBarry Smith 
14530de9b25SBarry Smith    Notes:
14630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
14730de9b25SBarry Smith 
14830de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
14930de9b25SBarry Smith 
15030de9b25SBarry Smith    Concepts: error^setting condition
15130de9b25SBarry Smith 
1526024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
15330de9b25SBarry Smith M*/
1544a2ae208SSatish Balay #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
15530de9b25SBarry Smith 
15630de9b25SBarry Smith /*MC
15730de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
15830de9b25SBarry Smith 
15930de9b25SBarry Smith    Not Collective
16030de9b25SBarry Smith 
16130de9b25SBarry Smith    Synopsis:
162a7cc72afSBarry Smith    void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
16330de9b25SBarry Smith 
16430de9b25SBarry Smith 
16530de9b25SBarry Smith    Input Parameters:
16630de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
16730de9b25SBarry Smith .  message - error message in the printf format
16830de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
16930de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
17030de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
17130de9b25SBarry Smith 
17230de9b25SBarry Smith   Level: beginner
17330de9b25SBarry Smith 
17430de9b25SBarry Smith    Notes:
17530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
17630de9b25SBarry Smith 
17730de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
17830de9b25SBarry Smith 
17930de9b25SBarry Smith    Concepts: error^setting condition
18030de9b25SBarry Smith 
1816024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
18230de9b25SBarry Smith M*/
1834a2ae208SSatish Balay #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
18430de9b25SBarry Smith 
1854a2ae208SSatish Balay #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
186a30c184eSMatthew Knepley #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
187a30c184eSMatthew 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);}
188a30c184eSMatthew 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);}
189e855a17bSBarry Smith #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
1909a00fa46SSatish Balay 
19130de9b25SBarry Smith /*MC
19230de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
19330de9b25SBarry Smith 
19430de9b25SBarry Smith    Not Collective
19530de9b25SBarry Smith 
19630de9b25SBarry Smith    Synopsis:
197a7cc72afSBarry Smith    void CHKERRQ(PetscErrorCode errorcode)
19830de9b25SBarry Smith 
19930de9b25SBarry Smith 
20030de9b25SBarry Smith    Input Parameters:
20130de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
20230de9b25SBarry Smith 
20330de9b25SBarry Smith   Level: beginner
20430de9b25SBarry Smith 
20530de9b25SBarry Smith    Notes:
20630de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
20730de9b25SBarry Smith 
20830de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2095324ea47SKris Buschelman 
210fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2115324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2125324ea47SKris Buschelman 
2135324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2145324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2155324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
216fcecf507SKris Buschelman     a more appropriate construct for using PETSc Error Handling would be
2175324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
21830de9b25SBarry Smith 
21930de9b25SBarry Smith    Concepts: error^setting condition
22030de9b25SBarry Smith 
22191d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
22230de9b25SBarry Smith M*/
223c3be3f59SMatthew Knepley #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
22430de9b25SBarry Smith 
22594a56cdfSMatthew Knepley #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
22694a56cdfSMatthew Knepley #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
22785614651SBarry Smith 
22830de9b25SBarry Smith /*MC
22930de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
23030de9b25SBarry Smith 
23130de9b25SBarry Smith    Not Collective
23230de9b25SBarry Smith 
23330de9b25SBarry Smith    Synopsis:
23491d3bdf4SKris Buschelman    CHKMEMQ;
23530de9b25SBarry Smith 
23630de9b25SBarry Smith   Level: beginner
23730de9b25SBarry Smith 
23830de9b25SBarry Smith    Notes:
239ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
24030de9b25SBarry Smith 
24130de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
24230de9b25SBarry Smith 
24330de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
24430de9b25SBarry Smith 
24530de9b25SBarry Smith    Concepts: memory corruption
24630de9b25SBarry Smith 
2476024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
248ff002950SBarry Smith           PetscMallocValidate()
24930de9b25SBarry Smith M*/
250ff002950SBarry Smith #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
25185614651SBarry Smith 
252*e68848bdSBarry Smith #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
253*e68848bdSBarry Smith 
254f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
2556849ba73SBarry Smith extern  PetscErrorCode __gierr;
256f1af5d2fSBarry Smith #define _   __gierr =
257ac355199SBarry Smith #define ___  CHKERRQ(__gierr);
258f1af5d2fSBarry Smith #endif
259f1af5d2fSBarry Smith 
260d5e45103SBarry Smith #define               PETSC_EXCEPTIONS_MAX  256
261d5e45103SBarry Smith extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
262d5e45103SBarry Smith extern PetscInt       PetscErrorUncatchableCount;
263d5e45103SBarry Smith extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
264d5e45103SBarry Smith extern PetscInt       PetscExceptionsCount;
265d5e45103SBarry Smith 
2666024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode);
267d5e45103SBarry Smith EXTERN void PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode);
268d5e45103SBarry Smith 
269c132512eSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
270c132512eSBarry Smith 
27175febeb1SBarry Smith /*MC
27275febeb1SBarry Smith    PetscExceptionCaught - Indicates if exception zierr was caught.
27375febeb1SBarry Smith 
27475febeb1SBarry Smith    Not Collective
27575febeb1SBarry Smith 
27675febeb1SBarry Smith    Synopsis:
27775febeb1SBarry Smith      PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
27875febeb1SBarry Smith 
27975febeb1SBarry Smith   Input Parameters:
28075febeb1SBarry Smith   + xierr - error code returned from PetscExceptionTry1()
28175febeb1SBarry Smith   - zierr - error code you want it to be
28275febeb1SBarry Smith 
28375febeb1SBarry Smith   Level: advanced
28475febeb1SBarry Smith 
28575febeb1SBarry Smith    Notes:
28675febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
28775febeb1SBarry Smith 
28875febeb1SBarry Smith   Concepts: exceptions, exception hanlding
28975febeb1SBarry Smith 
29075febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
29175febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
29275febeb1SBarry Smith M*/
2932a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
294d5e45103SBarry Smith   PetscInt i;
295d5e45103SBarry Smith   if (xierr != zierr) return PETSC_FALSE;
296d5e45103SBarry Smith   for (i=0; i<PetscErrorUncatchableCount; i++) {
297d5e45103SBarry Smith     if (PetscErrorUncatchable[i] == zierr) {
298d5e45103SBarry Smith       return PETSC_FALSE;
299d5e45103SBarry Smith     }
300d5e45103SBarry Smith   }
301d5e45103SBarry Smith   return PETSC_TRUE;
302d5e45103SBarry Smith }
303d5e45103SBarry Smith 
30475febeb1SBarry Smith /*MC
30575febeb1SBarry Smith    PetscExceptionValue - Indicates if the error code is one that is currently being tried
306d5e45103SBarry Smith 
30775febeb1SBarry Smith    Not Collective
30875febeb1SBarry Smith 
30975febeb1SBarry Smith    Synopsis:
31075febeb1SBarry Smith      PetscTruth PetscExceptionValue(PetscErrorCode xierr);
31175febeb1SBarry Smith 
31275febeb1SBarry Smith   Input Parameters:
31375febeb1SBarry Smith   . xierr - error code
31475febeb1SBarry Smith 
31575febeb1SBarry Smith   Level: developer
31675febeb1SBarry Smith 
31775febeb1SBarry Smith    Notes:
31875febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
31975febeb1SBarry Smith 
32075febeb1SBarry Smith   Concepts: exceptions, exception hanlding
32175febeb1SBarry Smith 
32275febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
32375febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
32475febeb1SBarry Smith M*/
3252a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
326d5e45103SBarry Smith   PetscInt i;
327d5e45103SBarry Smith   for (i=0; i<PetscExceptionsCount; i++) {
328d5e45103SBarry Smith     if (PetscExceptions[i] == zierr) {
329d5e45103SBarry Smith       return PETSC_TRUE;
330d5e45103SBarry Smith     }
331d5e45103SBarry Smith   }
332d5e45103SBarry Smith   return PETSC_FALSE;
333d5e45103SBarry Smith }
3346024bd2cSBarry Smith 
3356024bd2cSBarry Smith /*MC
3366024bd2cSBarry Smith    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
3376024bd2cSBarry Smith          rather than an error. That is if that error code is treated the program returns to this level,
3386024bd2cSBarry Smith          but does not call the error handlers
3396024bd2cSBarry Smith 
3406024bd2cSBarry Smith    Not Collective
3416024bd2cSBarry Smith 
3426024bd2cSBarry Smith    Synopsis:
3436024bd2cSBarry Smith      PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
3446024bd2cSBarry Smith 
3456024bd2cSBarry Smith   Level: advanced
3466024bd2cSBarry Smith 
3476024bd2cSBarry Smith    Notes:
3486024bd2cSBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
3496024bd2cSBarry Smith 
35075febeb1SBarry Smith   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
35175febeb1SBarry Smith         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
35275febeb1SBarry Smith         of how the local try is ignored if a higher (in the stack) one is also in effect.
3536024bd2cSBarry Smith 
3546024bd2cSBarry Smith   Concepts: exceptions, exception hanlding
3556024bd2cSBarry Smith 
3566024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
35775febeb1SBarry Smith           CHKERRQ(), PetscExceptionCaught()
3586024bd2cSBarry Smith M*/
3596024bd2cSBarry Smith extern PetscErrorCode PetscExceptionTmp;
360d5e45103SBarry Smith #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTmp)
3616024bd2cSBarry Smith 
36254a8ef01SBarry Smith #else
36390d37a7cSBarry Smith #define SETERRQ(n,s) ;
36490d37a7cSBarry Smith #define SETERRQ1(n,s,a1) ;
36590d37a7cSBarry Smith #define SETERRQ2(n,s,a1,a2) ;
36690d37a7cSBarry Smith #define SETERRQ3(n,s,a1,a2,a3) ;
36790d37a7cSBarry Smith #define SETERRQ4(n,s,a1,a2,a3,a4) ;
3689bd8d7fdSSatish Balay #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
369742f4fedSDinesh Kaushik #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
3701ee4faa0SMatthew Knepley #define SETERRABORT(comm,n,s) ;
37185614651SBarry Smith 
3724f227f7cSBarry Smith #define CHKERRQ(n)     ;
3731ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3741ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
37585614651SBarry Smith 
37685614651SBarry Smith #define CHKMEMQ        ;
37785614651SBarry Smith 
378f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
379f1af5d2fSBarry Smith #define _
380f1af5d2fSBarry Smith #define ___
381f1af5d2fSBarry Smith #endif
382f1af5d2fSBarry Smith 
383c132512eSBarry Smith #define PetscErrorSetCatchable(a,b) 0
384c132512eSBarry Smith #define PetscExceptionCaught(a,b)   PETSC_FALSE
385c132512eSBarry Smith #define PetscExceptionValue(a)      PETSC_FALSE
3866024bd2cSBarry Smith #define PetscExceptionTry1(a,b)     a
38754a8ef01SBarry Smith #endif
38854a8ef01SBarry Smith 
389ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void);
390ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **);
3916024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3926024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3936024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3946024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStopErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3956024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3966024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
3976024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
3986024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
399ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void);
400ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*);
401ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
402ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void);
403329f5518SBarry Smith 
404329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
405ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscSetFPTrap(PetscFPTrap);
40654a8ef01SBarry Smith 
4073a40ed3dSBarry Smith /*
4083a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4093a40ed3dSBarry Smith */
41051d3a450SBarry Smith #if defined(PETSC_USE_DEBUG)
4113a40ed3dSBarry Smith 
412184914b5SBarry Smith #define PETSCSTACKSIZE 15
413184914b5SBarry Smith 
4143a40ed3dSBarry Smith typedef struct  {
4150e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
4160e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
4170e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
418184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
419184914b5SBarry Smith         int currentsize;
4203a40ed3dSBarry Smith } PetscStack;
4213a40ed3dSBarry Smith 
422ff73aad6SKris Buschelman extern PETSC_DLLEXPORT PetscStack *petscstack;
423ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCopy(PetscStack*,PetscStack*);
424ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPrint(PetscStack*,FILE* fp);
425184914b5SBarry Smith 
426184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
4273a40ed3dSBarry Smith 
4286d385327SIbrahima Ba 
42930de9b25SBarry Smith /*MC
43030de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
43130de9b25SBarry Smith         used for error handling.
43230de9b25SBarry Smith 
43330de9b25SBarry Smith    Synopsis:
43430de9b25SBarry Smith    void PetscFunctionBegin;
43530de9b25SBarry Smith 
43630de9b25SBarry Smith    Usage:
43730de9b25SBarry Smith .vb
43830de9b25SBarry Smith      int something;
43930de9b25SBarry Smith 
44030de9b25SBarry Smith      PetscFunctionBegin;
44130de9b25SBarry Smith .ve
44230de9b25SBarry Smith 
44330de9b25SBarry Smith    Notes:
44430de9b25SBarry Smith      Not available in Fortran
44530de9b25SBarry Smith 
44630de9b25SBarry Smith    Level: developer
44730de9b25SBarry Smith 
44830de9b25SBarry Smith .seealso: PetscFunctionReturn()
44930de9b25SBarry Smith 
45030de9b25SBarry Smith .keywords: traceback, error handling
45130de9b25SBarry Smith M*/
4523a40ed3dSBarry Smith #define PetscFunctionBegin \
453beb17490SBarry Smith   {\
454184914b5SBarry Smith    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
4554a2ae208SSatish Balay     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
456184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = __FILE__; \
457184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = __SDIR__; \
458184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = __LINE__; \
459184914b5SBarry Smith     petscstack->currentsize++; \
4603a40ed3dSBarry Smith   }}
4613a40ed3dSBarry Smith 
4625cd90555SBarry Smith #define PetscStackPush(n) \
463184914b5SBarry Smith   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
464184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = n; \
465184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = "unknown"; \
466184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = "unknown"; \
467184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
468184914b5SBarry Smith     petscstack->currentsize++; \
4695cd90555SBarry Smith   }}
4703a40ed3dSBarry Smith 
471d64ed03dSBarry Smith #define PetscStackPop \
472184914b5SBarry Smith   {if (petscstack && petscstack->currentsize > 0) {     \
473184914b5SBarry Smith     petscstack->currentsize--; \
474184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
475184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
476184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
477184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
4785cd90555SBarry Smith   }};
479d64ed03dSBarry Smith 
48030de9b25SBarry Smith /*MC
48130de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
48230de9b25SBarry Smith         used for error handling. Replaces return()
48330de9b25SBarry Smith 
48430de9b25SBarry Smith    Synopsis:
48530de9b25SBarry Smith    void PetscFunctionReturn(0);
48630de9b25SBarry Smith 
48730de9b25SBarry Smith    Usage:
48830de9b25SBarry Smith .vb
48930de9b25SBarry Smith     ....
49030de9b25SBarry Smith      PetscFunctionReturn(0);
49130de9b25SBarry Smith    }
49230de9b25SBarry Smith .ve
49330de9b25SBarry Smith 
49430de9b25SBarry Smith    Notes:
49530de9b25SBarry Smith      Not available in Fortran
49630de9b25SBarry Smith 
49730de9b25SBarry Smith    Level: developer
49830de9b25SBarry Smith 
49930de9b25SBarry Smith .seealso: PetscFunctionBegin()
50030de9b25SBarry Smith 
50130de9b25SBarry Smith .keywords: traceback, error handling
50230de9b25SBarry Smith M*/
5035cd90555SBarry Smith #define PetscFunctionReturn(a) \
504beb17490SBarry Smith   {\
50506d1fe2cSBarry Smith   PetscStackPop; \
5065cd90555SBarry Smith   return(a);}
507d64ed03dSBarry Smith 
508ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
509ff94ddecSSatish Balay   {\
5101fceb228SKris Buschelman   PetscStackPop; \
5111fceb228SKris Buschelman   return;}
512ff94ddecSSatish Balay 
5136d385327SIbrahima Ba 
5146d385327SIbrahima Ba #else
5156d385327SIbrahima Ba 
5163a40ed3dSBarry Smith #define PetscFunctionBegin
5173a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
5185665465eSBarry Smith #define PetscFunctionReturnVoid() return
519d64ed03dSBarry Smith #define PetscStackPop
520d64ed03dSBarry Smith #define PetscStackPush(f)
521d64ed03dSBarry Smith #define PetscStackActive        0
5223a40ed3dSBarry Smith 
5233a40ed3dSBarry Smith #endif
5243a40ed3dSBarry Smith 
525ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCreate(void);
526ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackView(PetscViewer);
527ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDestroy(void);
528ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPublish(void);
529ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDepublish(void);
5303a40ed3dSBarry Smith 
53106d1fe2cSBarry Smith 
532e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
53306d1fe2cSBarry Smith #endif
534