[petsc-users] proper way to make petsc throw exceptions on errors?

Geoffrey Irving irving at naml.us
Thu Nov 14 12:15:45 CST 2013


On Thu, Nov 14, 2013 at 10:06 AM, Matthew Knepley <knepley at gmail.com> wrote:
> On Thu, Nov 14, 2013 at 12:02 PM, Geoffrey Irving <irving at naml.us> wrote:
>>
>> Is there a safe way to make petsc throw exceptions from C++ code,
>> without interfering with the same passage from errors through C code?
>> The existence of PETSC_ERROR_IN_CXX and PetscPushErrorHandler seems
>> promising?  Could I push a C++ error handler at the top of a C++
>> function and pop it and the end, for example?
>
>
> PyLith uses:
>
> #define PYLITH_CHECK_ERROR(err) do {if (PetscUnlikely(err))
> {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,err,PETSC_ERROR_IN_CXX,0);}}
> while(0)
>
> #define PYLITH_CHECK_ERROR_MSG(err, msg) \
>   if (err) { \
>
> PetscError(PETSC_COMM_SELF,__LINE__,__FUNCT__,__FILE__,err,PETSC_ERROR_IN_CXX,
> 0, " "); \
>     throw std::runtime_error(msg); }
>
>    Matt

Thanks, that'll work.  I guess my hope for a magical solution where I
didn't have to call the check macro blinded me to the obvious "pass
PETSC_ERROR_IN_CXX" myself path.

Minor curiosity: why does the _MSG version not use PetscUnlikely?

Geoffrey


More information about the petsc-users mailing list