[Xorp-hackers] Uncatchable exceptions

Ben Greear greearb at candelatech.com
Fri Jun 17 08:39:33 PDT 2011


On 06/17/2011 04:24 AM, Steven Simpson wrote:
> Hi Ben,
>
> I've just been struggling with C++'s rather slack exception system.  I was doing this:

I really hate C++ exceptions, and don't know how to use them well.

In general, I'd welcome code that removed exceptions and used error
return values instead to deal with error conditions.  That is liable
to be a lot of changes, but perhaps some pieces of code could be cleaned
up at a time.

For your particular issue, I'd do whatever is simplest unless you feel
like doing more serious exception removal work.

Thanks,
Ben


>
>    const XrlArgs&  in; // a parameter
>
>    try {
>      uint32_t delay;
>      in.get("boo_delay", delay);
>      ...
>    } catch (const XrlArgs:XrlAtomNotFound&  e) {
>      // Get other arguments.
>    }
>
> So obviously I was getting XrlAtomNotFound exceptions that were killing the process, and I added the try-catch to intercept them - but it wasn't working.
> Traces showed that the exception was certainly happening on [in.get], but the catch block never executed.
>
> The method I'm calling doesn't declare the exception I'm trying to catch:
>
>      void get(const char* n, uint32_t&  t) const throw (BadArgs);
>
> That method calls this one:
>
> const uint32_t&
> XrlArgs::get_uint32(const char* name) const throw (BadArgs)
> {
>      try {
> 	return get(XrlAtom(name, xrlatom_uint32)).uint32();
>      } catch (const XrlAtom::NoData&  e) {
>          xorp_throw(BadArgs, e.why());
>      } catch (const XrlAtom::WrongType&  e) {
>          xorp_throw(BadArgs, e.why());
>      }
> }
>
> …and you can see that that code calls get(const XrlAtom&), which is declared to throw XrlAtomNotFound.  But get_uint32 doesn't catch that exception itself.
> According to this (under "Exception Specifications"):
>
> <http://4thmouse.com/mystuff/articles/UsingExceptionsEffectively.html>
>
> …the uncaught exception simply terminates the program immediately; not even an unwinding of the stack, it seems!
>
> Two possible solutions are obvious: either propagate the declaration of XrlAtomNotFound, or catch it in this and related functions, and throw a BadArgs
> instead.  I suppose the latter would be much less disruptive.
>
> Incidentally, if the [get] call inside get_uint32 can only throw XrlAtomNotFound, can either of the existing catch clauses ever execute?
>
> Cheers,
>
> Steven
>
>
>
> _______________________________________________
> Xorp-hackers mailing list
> Xorp-hackers at icir.org
> http://mailman.ICSI.Berkeley.EDU/mailman/listinfo/xorp-hackers


-- 
Ben Greear <greearb at candelatech.com>
Candela Technologies Inc  http://www.candelatech.com



More information about the Xorp-hackers mailing list