best of both worlds for located exceptions?

27 views
Skip to first unread message

Bob Carpenter

unread,
Mar 9, 2015, 1:50:17 AM3/9/15
to stan...@googlegroups.com
Here's what I came up with. It doesn't require any policy
on Stan code and will use RTTI to create an appropriately
typed exception (that is, catchable by the nested exception
type) if it's in the standard library (option 1), falling back
on std::exception for other types (option 2).

I've attached the code and the test to show how it works
on its own. I also pushed to feature/issue-1195-runtime-error-lines
in case anyone wants to run the test.

My lingering uncertainty is whether it's safe to store the
nested type as a const reference and return it the same way
from the getter. It'd be easy enough to copy the nested exception
and return it by value rather than by reference. (One of the
current tests for reference equality will fail, though.)

My plan is to wrap the code blocks of all relevant functions
and the constructor as:

int line__ = -1;
try {
...run updating line number...
} catch (const std::exception& e) {
throw_located_exception(e,line__);
}

If the exception e is an instance of any of the std library
exception types, the rethrown exception will be catchable
by its type E or by located_exception<E>. I could add some
Boost types to the parade.

I also built in the inspection with RTTI for good measure
in case someone wants to just catch std::exception and use
RTTI to inspect the type of the nested exception and get it
dynamically cast to a given type.

I learned a lot about exceptions building this thing today!

- Bob

located_exception.hpp
located_exception_test.cpp

Ben Goodrich

unread,
Mar 9, 2015, 8:29:04 AM3/9/15
to stan...@googlegroups.com
Still not compiling without -std=c++11

goodrich@T540p:/tmp$ clang++-3.6 -fsyntax-only located_exception_test.cpp
In file included from located_exception_test.cpp:1:
./
located_exception.hpp:80:24: error: no member named 'to_string' in namespace 'std'
               
+ std::to_string(line)
                 
~~~~~^

instead do

            + static_cast<std::ostringstream*>( &(std::ostringstream() << line) )->str()

Ben

Bob Carpenter

unread,
Mar 9, 2015, 9:43:16 AM3/9/15
to stan...@googlegroups.com
Thanks -- forgot to fix that.

- Bob
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bob Carpenter

unread,
Mar 9, 2015, 9:47:18 AM3/9/15
to stan...@googlegroups.com
Fixed. I'd actually implemented the fix function but
hadn't plumbed it into the constructor.

- Bob

> On Mar 10, 2015, at 12:42 AM, Bob Carpenter <ca...@alias-i.com> wrote:
>
> Thanks -- forgot to fix that.
>
> - Bob
>
>> On Mar 9, 2015, at 11:29 PM, Ben Goodrich <goodri...@gmail.com> wrote:
>>

Marcus Brubaker

unread,
Mar 9, 2015, 12:09:30 PM3/9/15
to stan...@googlegroups.com
I read over the code quickly and two small things:

1. In located_exception::get() I think it should be a dynamic cast, not a static one.
2. In throw_located_exception() if it falls through to the std::exception case, it might be worth emitting a warning with the typeid of the exception that was thrown so that we know when this case gets hit.

Otherwise it looks great!

Cheers,
Marcus


>> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.

>> For more options, visit https://groups.google.com/d/optout.
>

--
You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+unsubscribe@googlegroups.com.

Bob Carpenter

unread,
Mar 9, 2015, 7:03:17 PM3/9/15
to stan...@googlegroups.com
Thanks! I'll make those fixes.

I think it would make sense to put the warning
in the exception itself --- I don't have anywhere
to write to within the located exception class!

- Bob
> >> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> >> For more options, visit https://groups.google.com/d/optout.
> >
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to the Google Groups "stan development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to stan-dev+u...@googlegroups.com.

Bob Carpenter

unread,
Mar 9, 2015, 7:12:17 PM3/9/15
to stan...@googlegroups.com
It also reminds me that it's a motivation to keep the use
of references --- otherwise the original exception class will
get sliced down to a std::exception!

- Bob
Reply all
Reply to author
Forward
0 new messages