xref: /petsc/include/petscerror.h (revision f621e05e188db6fe8da961e5af0df981dcc81528)
154a8ef01SBarry Smith /*
2*f621e05eSBarry 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}"'
14*f621e05eSBarry Smith    which is a flag passed to the C/C++ compilers. This declaration below
15*f621e05eSBarry 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
23*f621e05eSBarry Smith    in printing error messages. This is defined here in case the user
24*f621e05eSBarry 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
3445d48df9SBarry Smith 
3554a8ef01SBarry Smith */
3645d48df9SBarry Smith #define PETSC_ERR_MEM              55   /* unable to allocate requested memory */
37106f7b34SBarry Smith #define PETSC_ERR_MEM_MALLOC_0     85   /* cannot malloc zero size */
3847794344SBarry Smith #define PETSC_ERR_SUP              56   /* no support for requested operation */
39e2d1d2b7SBarry Smith #define PETSC_ERR_SUP_SYS          57   /* no support for requested operation on this computer system */
40e2d1d2b7SBarry Smith #define PETSC_ERR_ORDER            58   /* operation done in wrong order */
4145d48df9SBarry Smith #define PETSC_ERR_SIG              59   /* signal received */
42f1caa5a4SBarry Smith #define PETSC_ERR_FP               72   /* floating point exception */
43a8c6a408SBarry Smith #define PETSC_ERR_COR              74   /* corrupted PETSc object */
44a8c6a408SBarry Smith #define PETSC_ERR_LIB              76   /* error in library called by PETSc */
45329ffe3dSLois Curfman McInnes #define PETSC_ERR_PLIB             77   /* PETSc library generated inconsistent data */
46329ffe3dSLois Curfman McInnes #define PETSC_ERR_MEMC             78   /* memory corruption */
47b3cc6726SBarry Smith #define PETSC_ERR_CONV_FAILED      82   /* iterative method (KSP or SNES) failed */
481302d50aSBarry Smith #define PETSC_ERR_USER             83   /* user has not provided needed function */
4945d48df9SBarry Smith 
5045d48df9SBarry Smith #define PETSC_ERR_ARG_SIZ          60   /* nonconforming object sizes used in operation */
5145d48df9SBarry Smith #define PETSC_ERR_ARG_IDN          61   /* two arguments not allowed to be the same */
52a8c6a408SBarry Smith #define PETSC_ERR_ARG_WRONG        62   /* wrong argument (but object probably ok) */
5345d48df9SBarry Smith #define PETSC_ERR_ARG_CORRUPT      64   /* null or corrupted PETSc object as argument */
5445d48df9SBarry Smith #define PETSC_ERR_ARG_OUTOFRANGE   63   /* input argument, out of range */
554f227f7cSBarry Smith #define PETSC_ERR_ARG_BADPTR       68   /* invalid pointer argument */
564f227f7cSBarry Smith #define PETSC_ERR_ARG_NOTSAMETYPE  69   /* two args must be same object type */
576831982aSBarry Smith #define PETSC_ERR_ARG_NOTSAMECOMM  80   /* two args must be same communicators */
58d252947aSBarry Smith #define PETSC_ERR_ARG_WRONGSTATE   73   /* object in argument is in wrong state, e.g. unassembled mat */
59a8c6a408SBarry Smith #define PETSC_ERR_ARG_INCOMP       75   /* two arguments are incompatible */
604482741eSBarry Smith #define PETSC_ERR_ARG_NULL         85   /* argument is null that should not be */
61958c9bccSBarry Smith #define PETSC_ERR_ARG_UNKNOWN_TYPE 86   /* type name doesn't match any registered type */
62c2be2410SBarry Smith #define PETSC_ERR_ARG_DOMAIN       87   /* argument is not in domain of function */
634f227f7cSBarry Smith 
644f227f7cSBarry Smith #define PETSC_ERR_FILE_OPEN        65   /* unable to open file */
654f227f7cSBarry Smith #define PETSC_ERR_FILE_READ        66   /* unable to read from file */
664f227f7cSBarry Smith #define PETSC_ERR_FILE_WRITE       67   /* unable to write to file */
67a8c6a408SBarry Smith #define PETSC_ERR_FILE_UNEXPECTED  79   /* unexpected data in file */
6845d48df9SBarry Smith 
69329ffe3dSLois Curfman McInnes #define PETSC_ERR_MAT_LU_ZRPVT     71   /* detected a zero pivot during LU factorization */
709e3b2f23SBarry Smith #define PETSC_ERR_MAT_CH_ZRPVT     81   /* detected a zero pivot during Cholesky factorization */
7154a8ef01SBarry Smith 
724b55f32eSBarry Smith #if defined(PETSC_USE_ERRORCHECKING)
7330de9b25SBarry Smith 
7430de9b25SBarry Smith /*MC
7530de9b25SBarry Smith    SETERRQ - Macro that is called when an error has been detected,
7630de9b25SBarry Smith 
7730de9b25SBarry Smith    Not Collective
7830de9b25SBarry Smith 
7930de9b25SBarry Smith    Synopsis:
80a7cc72afSBarry Smith    void SETERRQ(PetscErrorCode errorcode,char *message)
8130de9b25SBarry Smith 
8230de9b25SBarry Smith 
8330de9b25SBarry Smith    Input Parameters:
8430de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
8530de9b25SBarry Smith -  message - error message
8630de9b25SBarry Smith 
8730de9b25SBarry Smith   Level: beginner
8830de9b25SBarry Smith 
8930de9b25SBarry Smith    Notes:
9030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
9130de9b25SBarry Smith 
9230de9b25SBarry Smith     See SETERRQ1(), SETERRQ2(), SETERRQ3() for versions that take arguments
9330de9b25SBarry Smith 
9430de9b25SBarry Smith 
9530de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
9630de9b25SBarry Smith 
9730de9b25SBarry Smith    Concepts: error^setting condition
9830de9b25SBarry Smith 
9991d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3()
10030de9b25SBarry Smith M*/
1014a2ae208SSatish Balay #define SETERRQ(n,s)              {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);}
10230de9b25SBarry Smith 
10330de9b25SBarry Smith /*MC
10430de9b25SBarry Smith    SETERRQ1 - Macro that is called when an error has been detected,
10530de9b25SBarry Smith 
10630de9b25SBarry Smith    Not Collective
10730de9b25SBarry Smith 
10830de9b25SBarry Smith    Synopsis:
109a7cc72afSBarry Smith    void SETERRQ1(PetscErrorCode errorcode,char *formatmessage,arg)
11030de9b25SBarry Smith 
11130de9b25SBarry Smith 
11230de9b25SBarry Smith    Input Parameters:
11330de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
11430de9b25SBarry Smith .  message - error message in the printf format
11530de9b25SBarry Smith -  arg - argument (for example an integer, string or double)
11630de9b25SBarry Smith 
11730de9b25SBarry Smith   Level: beginner
11830de9b25SBarry Smith 
11930de9b25SBarry Smith    Notes:
12030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
12130de9b25SBarry Smith 
12230de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
12330de9b25SBarry Smith 
12430de9b25SBarry Smith    Concepts: error^setting condition
12530de9b25SBarry Smith 
12691d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ(), SETERRQ2(), SETERRQ3()
12730de9b25SBarry Smith M*/
1284a2ae208SSatish Balay #define SETERRQ1(n,s,a1)          {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1);}
12930de9b25SBarry Smith 
13030de9b25SBarry Smith /*MC
13130de9b25SBarry Smith    SETERRQ2 - Macro that is called when an error has been detected,
13230de9b25SBarry Smith 
13330de9b25SBarry Smith    Not Collective
13430de9b25SBarry Smith 
13530de9b25SBarry Smith    Synopsis:
136a7cc72afSBarry Smith    void SETERRQ2(PetscErrorCode errorcode,char *formatmessage,arg1,arg2)
13730de9b25SBarry Smith 
13830de9b25SBarry Smith 
13930de9b25SBarry Smith    Input Parameters:
14030de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
14130de9b25SBarry Smith .  message - error message in the printf format
14230de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
14330de9b25SBarry Smith -  arg2 - argument (for example an integer, string or double)
14430de9b25SBarry Smith 
14530de9b25SBarry Smith   Level: beginner
14630de9b25SBarry Smith 
14730de9b25SBarry Smith    Notes:
14830de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
14930de9b25SBarry Smith 
15030de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
15130de9b25SBarry Smith 
15230de9b25SBarry Smith    Concepts: error^setting condition
15330de9b25SBarry Smith 
1546024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ3()
15530de9b25SBarry Smith M*/
1564a2ae208SSatish Balay #define SETERRQ2(n,s,a1,a2)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2);}
15730de9b25SBarry Smith 
15830de9b25SBarry Smith /*MC
15930de9b25SBarry Smith    SETERRQ3 - Macro that is called when an error has been detected,
16030de9b25SBarry Smith 
16130de9b25SBarry Smith    Not Collective
16230de9b25SBarry Smith 
16330de9b25SBarry Smith    Synopsis:
164a7cc72afSBarry Smith    void SETERRQ3(PetscErrorCode errorcode,char *formatmessage,arg1,arg2,arg3)
16530de9b25SBarry Smith 
16630de9b25SBarry Smith 
16730de9b25SBarry Smith    Input Parameters:
16830de9b25SBarry Smith +  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
16930de9b25SBarry Smith .  message - error message in the printf format
17030de9b25SBarry Smith .  arg1 - argument (for example an integer, string or double)
17130de9b25SBarry Smith .  arg2 - argument (for example an integer, string or double)
17230de9b25SBarry Smith -  arg3 - argument (for example an integer, string or double)
17330de9b25SBarry Smith 
17430de9b25SBarry Smith   Level: beginner
17530de9b25SBarry Smith 
17630de9b25SBarry Smith    Notes:
17730de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
17830de9b25SBarry Smith 
179*f621e05eSBarry Smith     There are also versions for 4, 5, 6 and 7 arguments.
180*f621e05eSBarry Smith 
18130de9b25SBarry Smith    Experienced users can set the error handler with PetscPushErrorHandler().
18230de9b25SBarry Smith 
18330de9b25SBarry Smith    Concepts: error^setting condition
18430de9b25SBarry Smith 
1856024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), CHKERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2()
18630de9b25SBarry Smith M*/
1874a2ae208SSatish Balay #define SETERRQ3(n,s,a1,a2,a3)    {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3);}
18830de9b25SBarry Smith 
1894a2ae208SSatish Balay #define SETERRQ4(n,s,a1,a2,a3,a4) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4);}
190a30c184eSMatthew Knepley #define SETERRQ5(n,s,a1,a2,a3,a4,a5)       {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s,a1,a2,a3,a4,a5);}
191a30c184eSMatthew 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);}
192a30c184eSMatthew 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);}
193e855a17bSBarry Smith #define SETERRABORT(comm,n,s)     {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,1,s);MPI_Abort(comm,n);}
1949a00fa46SSatish Balay 
19530de9b25SBarry Smith /*MC
19630de9b25SBarry Smith    CHKERRQ - Checks error code, if non-zero it calls the error handler and then returns
19730de9b25SBarry Smith 
19830de9b25SBarry Smith    Not Collective
19930de9b25SBarry Smith 
20030de9b25SBarry Smith    Synopsis:
201a7cc72afSBarry Smith    void CHKERRQ(PetscErrorCode errorcode)
20230de9b25SBarry Smith 
20330de9b25SBarry Smith 
20430de9b25SBarry Smith    Input Parameters:
20530de9b25SBarry Smith .  errorcode - nonzero error code, see the list of standard error codes in include/petscerror.h
20630de9b25SBarry Smith 
20730de9b25SBarry Smith   Level: beginner
20830de9b25SBarry Smith 
20930de9b25SBarry Smith    Notes:
21030de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
21130de9b25SBarry Smith 
21230de9b25SBarry Smith     Experienced users can set the error handler with PetscPushErrorHandler().
2135324ea47SKris Buschelman 
214fcecf507SKris Buschelman     CHKERRQ(n) is fundamentally a macro replacement for
2155324ea47SKris Buschelman          if (n) return(PetscError(...,n,...));
2165324ea47SKris Buschelman 
2175324ea47SKris Buschelman     Although typical usage resembles "void CHKERRQ(PetscErrorCode)" as described above, for certain uses it is
2185324ea47SKris Buschelman     highly inappropriate to use it in this manner as it invokes return(PetscErrorCode). In particular,
2195324ea47SKris Buschelman     it cannot be used in functions which return(void) or any other datatype.  In these types of functions,
220fcecf507SKris Buschelman     a more appropriate construct for using PETSc Error Handling would be
2215324ea47SKris Buschelman          if (n) {PetscError(....); return(YourReturnType);}
22230de9b25SBarry Smith 
22330de9b25SBarry Smith    Concepts: error^setting condition
22430de9b25SBarry Smith 
22591d3bdf4SKris Buschelman .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ2()
22630de9b25SBarry Smith M*/
227c3be3f59SMatthew Knepley #define CHKERRQ(n)             if (n) {return PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
22830de9b25SBarry Smith 
22994a56cdfSMatthew Knepley #define CHKERRABORT(comm,n)    if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");MPI_Abort(comm,n);}
23094a56cdfSMatthew Knepley #define CHKERRCONTINUE(n)      if (n) {PetscError(__LINE__,__FUNCT__,__FILE__,__SDIR__,n,0," ");}
23185614651SBarry Smith 
23230de9b25SBarry Smith /*MC
23330de9b25SBarry Smith    CHKMEMQ - Checks the memory for corruption, calls error handler if any is detected
23430de9b25SBarry Smith 
23530de9b25SBarry Smith    Not Collective
23630de9b25SBarry Smith 
23730de9b25SBarry Smith    Synopsis:
23891d3bdf4SKris Buschelman    CHKMEMQ;
23930de9b25SBarry Smith 
24030de9b25SBarry Smith   Level: beginner
24130de9b25SBarry Smith 
24230de9b25SBarry Smith    Notes:
243ff002950SBarry Smith     Must run with the option -malloc_debug to enable this option
24430de9b25SBarry Smith 
24530de9b25SBarry Smith     Once the error handler is called the calling function is then returned from with the given error code.
24630de9b25SBarry Smith 
24730de9b25SBarry Smith     By defaults prints location where memory that is corrupted was allocated.
24830de9b25SBarry Smith 
249*f621e05eSBarry Smith     Use CHKMEMA for functions that return void
250*f621e05eSBarry Smith 
25130de9b25SBarry Smith    Concepts: memory corruption
25230de9b25SBarry Smith 
2536024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
254ff002950SBarry Smith           PetscMallocValidate()
25530de9b25SBarry Smith M*/
256ff002950SBarry Smith #define CHKMEMQ {PetscErrorCode _7_ierr = PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);CHKERRQ(_7_ierr);}
25785614651SBarry Smith 
258e68848bdSBarry Smith #define CHKMEMA {PetscMallocValidate(__LINE__,__FUNCT__,__FILE__,__SDIR__);}
259e68848bdSBarry Smith 
260f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
2616849ba73SBarry Smith extern  PetscErrorCode __gierr;
262f1af5d2fSBarry Smith #define _   __gierr =
263ac355199SBarry Smith #define ___  CHKERRQ(__gierr);
264f1af5d2fSBarry Smith #endif
265f1af5d2fSBarry Smith 
266d5e45103SBarry Smith #define               PETSC_EXCEPTIONS_MAX  256
267d5e45103SBarry Smith extern PetscErrorCode PetscErrorUncatchable[PETSC_EXCEPTIONS_MAX];
268d5e45103SBarry Smith extern PetscInt       PetscErrorUncatchableCount;
269d5e45103SBarry Smith extern PetscErrorCode PetscExceptions[PETSC_EXCEPTIONS_MAX];
270d5e45103SBarry Smith extern PetscInt       PetscExceptionsCount;
271d5e45103SBarry Smith 
2726024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscExceptionPush(PetscErrorCode);
273d5e45103SBarry Smith EXTERN void PETSC_DLLEXPORT PetscExceptionPop(PetscErrorCode);
274d5e45103SBarry Smith 
275c132512eSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorSetCatchable(PetscErrorCode,PetscTruth);
276c132512eSBarry Smith 
27775febeb1SBarry Smith /*MC
27875febeb1SBarry Smith    PetscExceptionCaught - Indicates if exception zierr was caught.
27975febeb1SBarry Smith 
28075febeb1SBarry Smith    Not Collective
28175febeb1SBarry Smith 
28275febeb1SBarry Smith    Synopsis:
28375febeb1SBarry Smith      PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr);
28475febeb1SBarry Smith 
28575febeb1SBarry Smith   Input Parameters:
28675febeb1SBarry Smith   + xierr - error code returned from PetscExceptionTry1()
28775febeb1SBarry Smith   - zierr - error code you want it to be
28875febeb1SBarry Smith 
28975febeb1SBarry Smith   Level: advanced
29075febeb1SBarry Smith 
29175febeb1SBarry Smith    Notes:
29275febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
29375febeb1SBarry Smith 
29475febeb1SBarry Smith   Concepts: exceptions, exception hanlding
29575febeb1SBarry Smith 
29675febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
29775febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionValue()
29875febeb1SBarry Smith M*/
2992a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionCaught(PetscErrorCode xierr,PetscErrorCode zierr) {
300d5e45103SBarry Smith   PetscInt i;
301d5e45103SBarry Smith   if (xierr != zierr) return PETSC_FALSE;
302d5e45103SBarry Smith   for (i=0; i<PetscErrorUncatchableCount; i++) {
303d5e45103SBarry Smith     if (PetscErrorUncatchable[i] == zierr) {
304d5e45103SBarry Smith       return PETSC_FALSE;
305d5e45103SBarry Smith     }
306d5e45103SBarry Smith   }
307d5e45103SBarry Smith   return PETSC_TRUE;
308d5e45103SBarry Smith }
309d5e45103SBarry Smith 
31075febeb1SBarry Smith /*MC
31175febeb1SBarry Smith    PetscExceptionValue - Indicates if the error code is one that is currently being tried
312d5e45103SBarry Smith 
31375febeb1SBarry Smith    Not Collective
31475febeb1SBarry Smith 
31575febeb1SBarry Smith    Synopsis:
31675febeb1SBarry Smith      PetscTruth PetscExceptionValue(PetscErrorCode xierr);
31775febeb1SBarry Smith 
31875febeb1SBarry Smith   Input Parameters:
31975febeb1SBarry Smith   . xierr - error code
32075febeb1SBarry Smith 
32175febeb1SBarry Smith   Level: developer
32275febeb1SBarry Smith 
32375febeb1SBarry Smith    Notes:
32475febeb1SBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
32575febeb1SBarry Smith 
32675febeb1SBarry Smith   Concepts: exceptions, exception hanlding
32775febeb1SBarry Smith 
32875febeb1SBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
32975febeb1SBarry Smith           CHKERRQ(), PetscExceptionTry1(), PetscExceptionCaught()
33075febeb1SBarry Smith M*/
3312a6677a3SSatish Balay PETSC_STATIC_INLINE PetscTruth PetscExceptionValue(PetscErrorCode zierr) {
332d5e45103SBarry Smith   PetscInt i;
333d5e45103SBarry Smith   for (i=0; i<PetscExceptionsCount; i++) {
334d5e45103SBarry Smith     if (PetscExceptions[i] == zierr) {
335d5e45103SBarry Smith       return PETSC_TRUE;
336d5e45103SBarry Smith     }
337d5e45103SBarry Smith   }
338d5e45103SBarry Smith   return PETSC_FALSE;
339d5e45103SBarry Smith }
3406024bd2cSBarry Smith 
3416024bd2cSBarry Smith /*MC
3426024bd2cSBarry Smith    PetscExceptionTry1 - Runs the routine, causing a particular error code to be treated as an exception,
3436024bd2cSBarry Smith          rather than an error. That is if that error code is treated the program returns to this level,
3446024bd2cSBarry Smith          but does not call the error handlers
3456024bd2cSBarry Smith 
3466024bd2cSBarry Smith    Not Collective
3476024bd2cSBarry Smith 
3486024bd2cSBarry Smith    Synopsis:
3496024bd2cSBarry Smith      PetscExceptionTry1(PetscErrorCode routine(....),PetscErrorCode);
3506024bd2cSBarry Smith 
3516024bd2cSBarry Smith   Level: advanced
3526024bd2cSBarry Smith 
3536024bd2cSBarry Smith    Notes:
3546024bd2cSBarry Smith     PETSc must not be configured using the option --with-errorchecking=0 for this to work
3556024bd2cSBarry Smith 
35675febeb1SBarry Smith   Note: In general, the outer most try on an exception is the one that will be caught (that is trys down in
35775febeb1SBarry Smith         PETSc code will not usually handle an exception that was issued above). See SNESSolve() for an example
35875febeb1SBarry Smith         of how the local try is ignored if a higher (in the stack) one is also in effect.
3596024bd2cSBarry Smith 
3606024bd2cSBarry Smith   Concepts: exceptions, exception hanlding
3616024bd2cSBarry Smith 
3626024bd2cSBarry Smith .seealso: PetscTraceBackErrorHandler(), PetscPushErrorHandler(), PetscError(), SETERRQ(), CHKMEMQ, SETERRQ1(), SETERRQ2(), SETERRQ3(),
36375febeb1SBarry Smith           CHKERRQ(), PetscExceptionCaught()
3646024bd2cSBarry Smith M*/
3656024bd2cSBarry Smith extern PetscErrorCode PetscExceptionTmp;
366d5e45103SBarry Smith #define PetscExceptionTry1(a,b) (PetscExceptionTmp = PetscExceptionPush(b)) ? PetscExceptionTmp : (PetscExceptionTmp = a , PetscExceptionPop(b),PetscExceptionTmp)
3676024bd2cSBarry Smith 
36854a8ef01SBarry Smith #else
369*f621e05eSBarry Smith 
370*f621e05eSBarry Smith /*
371*f621e05eSBarry Smith     These are defined to be empty for when error checking is turned off, with config/configure.py --with-errorchecking=0
372*f621e05eSBarry Smith */
373*f621e05eSBarry Smith 
37490d37a7cSBarry Smith #define SETERRQ(n,s) ;
37590d37a7cSBarry Smith #define SETERRQ1(n,s,a1) ;
37690d37a7cSBarry Smith #define SETERRQ2(n,s,a1,a2) ;
37790d37a7cSBarry Smith #define SETERRQ3(n,s,a1,a2,a3) ;
37890d37a7cSBarry Smith #define SETERRQ4(n,s,a1,a2,a3,a4) ;
3799bd8d7fdSSatish Balay #define SETERRQ5(n,s,a1,a2,a3,a4,a5) ;
380742f4fedSDinesh Kaushik #define SETERRQ6(n,s,a1,a2,a3,a4,a5,a6) ;
3811ee4faa0SMatthew Knepley #define SETERRABORT(comm,n,s) ;
38285614651SBarry Smith 
3834f227f7cSBarry Smith #define CHKERRQ(n)     ;
3841ee4faa0SMatthew Knepley #define CHKERRABORT(comm,n) ;
3851ee4faa0SMatthew Knepley #define CHKERRCONTINUE(n) ;
38685614651SBarry Smith 
38785614651SBarry Smith #define CHKMEMQ        ;
38885614651SBarry Smith 
389f1af5d2fSBarry Smith #if !defined(PETSC_SKIP_UNDERSCORE_CHKERR)
390f1af5d2fSBarry Smith #define _
391f1af5d2fSBarry Smith #define ___
392f1af5d2fSBarry Smith #endif
393f1af5d2fSBarry Smith 
394c132512eSBarry Smith #define PetscErrorSetCatchable(a,b) 0
395c132512eSBarry Smith #define PetscExceptionCaught(a,b)   PETSC_FALSE
396c132512eSBarry Smith #define PetscExceptionValue(a)      PETSC_FALSE
3976024bd2cSBarry Smith #define PetscExceptionTry1(a,b)     a
39854a8ef01SBarry Smith #endif
39954a8ef01SBarry Smith 
400ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorPrintfInitialize(void);
401ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscErrorMessage(int,const char*[],char **);
4026024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscTraceBackErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4036024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscIgnoreErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4046024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscEmacsClientErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4056024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscStopErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4066024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAbortErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4076024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscAttachDebuggerErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
408e93bc3c1Svictor EXTERN PetscErrorCode PETSC_DLLEXPORT PetscReturnErrorHandler(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*);
4096024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscError(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,...) PETSC_PRINTF_FORMAT_CHECK(7,8);
4106024bd2cSBarry Smith EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushErrorHandler(PetscErrorCode (*handler)(int,const char*,const char*,const char*,PetscErrorCode,int,const char*,void*),void*);
411ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopErrorHandler(void);
412ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscDefaultSignalHandler(int,void*);
413ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPushSignalHandler(PetscErrorCode (*)(int,void *),void*);
414ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT PetscPopSignalHandler(void);
415329f5518SBarry Smith 
416329f5518SBarry Smith typedef enum {PETSC_FP_TRAP_OFF=0,PETSC_FP_TRAP_ON=1} PetscFPTrap;
417ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscSetFPTrap(PetscFPTrap);
41854a8ef01SBarry Smith 
4193a40ed3dSBarry Smith /*
4203a40ed3dSBarry Smith       Allows the code to build a stack frame as it runs
4213a40ed3dSBarry Smith */
42251d3a450SBarry Smith #if defined(PETSC_USE_DEBUG)
4233a40ed3dSBarry Smith 
424184914b5SBarry Smith #define PETSCSTACKSIZE 15
425184914b5SBarry Smith 
4263a40ed3dSBarry Smith typedef struct  {
4270e33f6ddSBarry Smith   const char *function[PETSCSTACKSIZE];
4280e33f6ddSBarry Smith   const char *file[PETSCSTACKSIZE];
4290e33f6ddSBarry Smith   const char *directory[PETSCSTACKSIZE];
430184914b5SBarry Smith         int  line[PETSCSTACKSIZE];
431184914b5SBarry Smith         int currentsize;
4323a40ed3dSBarry Smith } PetscStack;
4333a40ed3dSBarry Smith 
434ff73aad6SKris Buschelman extern PETSC_DLLEXPORT PetscStack *petscstack;
435ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCopy(PetscStack*,PetscStack*);
436ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPrint(PetscStack*,FILE* fp);
437184914b5SBarry Smith 
438184914b5SBarry Smith #define PetscStackActive (petscstack != 0)
4393a40ed3dSBarry Smith 
4406d385327SIbrahima Ba 
44130de9b25SBarry Smith /*MC
44230de9b25SBarry Smith    PetscFunctionBegin - First executable line of each PETSc function
44330de9b25SBarry Smith         used for error handling.
44430de9b25SBarry Smith 
44530de9b25SBarry Smith    Synopsis:
44630de9b25SBarry Smith    void PetscFunctionBegin;
44730de9b25SBarry Smith 
44830de9b25SBarry Smith    Usage:
44930de9b25SBarry Smith .vb
45030de9b25SBarry Smith      int something;
45130de9b25SBarry Smith 
45230de9b25SBarry Smith      PetscFunctionBegin;
45330de9b25SBarry Smith .ve
45430de9b25SBarry Smith 
45530de9b25SBarry Smith    Notes:
45630de9b25SBarry Smith      Not available in Fortran
45730de9b25SBarry Smith 
45830de9b25SBarry Smith    Level: developer
45930de9b25SBarry Smith 
46030de9b25SBarry Smith .seealso: PetscFunctionReturn()
46130de9b25SBarry Smith 
46230de9b25SBarry Smith .keywords: traceback, error handling
46330de9b25SBarry Smith M*/
4643a40ed3dSBarry Smith #define PetscFunctionBegin \
465beb17490SBarry Smith   {\
466184914b5SBarry Smith    if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
4674a2ae208SSatish Balay     petscstack->function[petscstack->currentsize]  = __FUNCT__; \
468184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = __FILE__; \
469184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = __SDIR__; \
470184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = __LINE__; \
471184914b5SBarry Smith     petscstack->currentsize++; \
4723a40ed3dSBarry Smith   }}
4733a40ed3dSBarry Smith 
4745cd90555SBarry Smith #define PetscStackPush(n) \
475184914b5SBarry Smith   {if (petscstack && (petscstack->currentsize < PETSCSTACKSIZE)) {    \
476184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = n; \
477184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = "unknown"; \
478184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = "unknown"; \
479184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
480184914b5SBarry Smith     petscstack->currentsize++; \
4815cd90555SBarry Smith   }}
4823a40ed3dSBarry Smith 
483d64ed03dSBarry Smith #define PetscStackPop \
484184914b5SBarry Smith   {if (petscstack && petscstack->currentsize > 0) {     \
485184914b5SBarry Smith     petscstack->currentsize--; \
486184914b5SBarry Smith     petscstack->function[petscstack->currentsize]  = 0; \
487184914b5SBarry Smith     petscstack->file[petscstack->currentsize]      = 0; \
488184914b5SBarry Smith     petscstack->directory[petscstack->currentsize] = 0; \
489184914b5SBarry Smith     petscstack->line[petscstack->currentsize]      = 0; \
4905cd90555SBarry Smith   }};
491d64ed03dSBarry Smith 
49230de9b25SBarry Smith /*MC
49330de9b25SBarry Smith    PetscFunctionReturn - Last executable line of each PETSc function
49430de9b25SBarry Smith         used for error handling. Replaces return()
49530de9b25SBarry Smith 
49630de9b25SBarry Smith    Synopsis:
49730de9b25SBarry Smith    void PetscFunctionReturn(0);
49830de9b25SBarry Smith 
49930de9b25SBarry Smith    Usage:
50030de9b25SBarry Smith .vb
50130de9b25SBarry Smith     ....
50230de9b25SBarry Smith      PetscFunctionReturn(0);
50330de9b25SBarry Smith    }
50430de9b25SBarry Smith .ve
50530de9b25SBarry Smith 
50630de9b25SBarry Smith    Notes:
50730de9b25SBarry Smith      Not available in Fortran
50830de9b25SBarry Smith 
50930de9b25SBarry Smith    Level: developer
51030de9b25SBarry Smith 
51130de9b25SBarry Smith .seealso: PetscFunctionBegin()
51230de9b25SBarry Smith 
51330de9b25SBarry Smith .keywords: traceback, error handling
51430de9b25SBarry Smith M*/
5155cd90555SBarry Smith #define PetscFunctionReturn(a) \
516beb17490SBarry Smith   {\
51706d1fe2cSBarry Smith   PetscStackPop; \
5185cd90555SBarry Smith   return(a);}
519d64ed03dSBarry Smith 
520ff94ddecSSatish Balay #define PetscFunctionReturnVoid() \
521ff94ddecSSatish Balay   {\
5221fceb228SKris Buschelman   PetscStackPop; \
5231fceb228SKris Buschelman   return;}
524ff94ddecSSatish Balay 
5256d385327SIbrahima Ba 
5266d385327SIbrahima Ba #else
5276d385327SIbrahima Ba 
5283a40ed3dSBarry Smith #define PetscFunctionBegin
5293a40ed3dSBarry Smith #define PetscFunctionReturn(a)  return(a)
5305665465eSBarry Smith #define PetscFunctionReturnVoid() return
531d64ed03dSBarry Smith #define PetscStackPop
532d64ed03dSBarry Smith #define PetscStackPush(f)
533d64ed03dSBarry Smith #define PetscStackActive        0
5343a40ed3dSBarry Smith 
5353a40ed3dSBarry Smith #endif
5363a40ed3dSBarry Smith 
537ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackCreate(void);
538ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackView(PetscViewer);
539ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDestroy(void);
540ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackPublish(void);
541ff73aad6SKris Buschelman EXTERN PetscErrorCode PETSC_DLLEXPORT  PetscStackDepublish(void);
5423a40ed3dSBarry Smith 
54306d1fe2cSBarry Smith 
544e9fa29b7SSatish Balay PETSC_EXTERN_CXX_END
54506d1fe2cSBarry Smith #endif
546