[petsc-dev] CHKERRXX() to propagate errors from C

Matthew Knepley knepley at gmail.com
Wed Oct 19 14:57:03 CDT 2011


On Wed, Oct 19, 2011 at 7:49 PM, Barry Smith <bsmith at mcs.anl.gov> wrote:

>
> On Oct 19, 2011, at 2:45 PM, Matthew Knepley wrote:
>
> > On Wed, Oct 19, 2011 at 7:39 PM, Jed Brown <jedbrown at mcs.anl.gov> wrote:
> > #define CHKERRXX(n)            do {if (PetscUnlikely(n))
> {PetscError(PETSC_COMM_SELF,__LINE__,PETSC_FUNCTION_NAME,__FILE__,__SDIR__,n,PETSC_ERROR_IN_CXX,0);}}
> while(0)
> >
> > Inside PetscError():
> >
> >   if (p == PETSC_ERROR_IN_CXX) {
> >     const char *str;
> >     if (eh->ctx) {
> >       std::ostringstream *msg;
> >       msg = (std::ostringstream*) eh->ctx;
> >       str = msg->str().c_str();
> >     } else {
> >       str = "Error detected in C PETSc";
> >     }
> >     throw PETSc::Exception(str);
> >   }
> >
> > This causes SEGV because eh is NULL when propagating an error from C (the
> caller checks a return code using CHKERRXX). This seems like a logic error,
> but I don't know what was intended.
> >
> > I changed this to if (eh && eh->ctx) which fixes the SEGV, but I don't
> know if something else was intended. I only pushed this to petsc-dev, but
> once we decide on the "right" fix, it should be ported to petsc-3.2.
> >
> > This was intended to get PETSc-like error checking from C++-style
> functions that throw exceptions. This was mostly for code I
> > wrote I think. Your fix is fine.
> >
> > After several years of this, I am now of the opinion that exceptions suck
> all the way, and I should rewrite all my C++ to use
> > return values. The stack does not get wiped out, you can still catch
> specific errors, and propagate ones you do not catch.
> > Does anyone have any serious defense of exceptions?
>
>     The properly manage the clean up of partially created objects etc don't
> they, which our C approach cannot handle. Not that I'm advocating for using
> them or not.


Hmm, I do not see how. As far as I know, you need logic to do that once you
catch them, which we could also put in.

   Matt


>
>   Barry
>
> >
> > I think we should retain the code for interoperability with C++-heads.
> >
> >    Matt
> >
> > --
> > What most experimenters take for granted before they begin their
> experiments is infinitely more interesting than any results to which their
> experiments lead.
> > -- Norbert Wiener
>
>


-- 
What most experimenters take for granted before they begin their experiments
is infinitely more interesting than any results to which their experiments
lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-dev/attachments/20111019/7f024954/attachment.html>


More information about the petsc-dev mailing list