trapping exceptions in R

131 views
Skip to first unread message

Bob Carpenter

unread,
Sep 2, 2012, 6:14:00 PM9/2/12
to stan...@googlegroups.com
I think we only need a try/catch arrangement to
catch the index out of bounds errors. Unlike the
domain errors, they don't get trapped and treated
as zero probability by the sampler.

Daniel or I can help if you (Jiqiang) need some
help with try/catch.

I believe the index exceptions are the only ones
we throw, but we should check.

- Bob

On 9/1/12 5:13 PM, Michael Braun wrote:
> Fixed the problem. Once I exported the data, and ran stan from a shell, I got the following helpful message:
>
> Exception: INDEX OPERATOR [] OUT OF BOUNDS; index=5; lower bound=1; upper bound=4; index position=1; xi
>
> Diagnostic information:
> Dynamic exception type: std::out_of_range
> std::exception::what: INDEX OPERATOR [] OUT OF BOUNDS; index=5; lower bound=1; upper bound=4; index position=1; xi
>
> This told me that I was interpreting the statement
>
> vector[N] xi[k]
>
> incorrectly. I thought it meant a vector of N k-dimensional arrays, when in fact it is a vector of k N-dimensional
> arrays. Once I fixed that, it all worked.
>
> It might be useful for those messages to pass through the rstan package somehow.

Jiqiang Guo

unread,
Sep 2, 2012, 8:51:23 PM9/2/12
to stan...@googlegroups.com
Please see comments below. 

Jiqiang

On Sun, Sep 2, 2012 at 6:14 PM, Bob Carpenter <ca...@alias-i.com> wrote:
I think we only need a try/catch arrangement to
catch the index out of bounds errors.  Unlike the
domain errors, they don't get trapped and treated
as zero probability by the sampler.

Daniel or I can help if you (Jiqiang) need some
help with try/catch.
 
Well, I have the try/catch to catch all exceptions and pass the error message to R. It is like

try {

} catch (std::exception& e) {
  // pass the error to R
} catch (...)  {
  // print the error message in R 
}

So I do not think I can do better, but I hope I understood wrongly so we can prevent R from crashing sometimes.  Ben thinks the message is passed to R, but R crashes before that. 



I believe the index exceptions are the only ones
we throw, but we should check.

- Bob

On 9/1/12 5:13 PM, Michael Braun wrote:
Fixed the problem.  Once I exported the data, and ran stan from a shell, I got the following helpful message:

Exception: INDEX OPERATOR [] OUT OF BOUNDS; index=5; lower bound=1; upper bound=4; index position=1; xi

Diagnostic information:
Dynamic exception type: std::out_of_range
std::exception::what: INDEX OPERATOR [] OUT OF BOUNDS; index=5; lower bound=1; upper bound=4; index position=1; xi

This told me that I was interpreting the statement

  vector[N] xi[k]

incorrectly.  I thought it meant a vector of N k-dimensional arrays, when in fact it is a vector of k N-dimensional
arrays. Once I fixed that, it all worked.

It might be useful for those messages to pass through the rstan package somehow.

--



Ben Goodrich

unread,
Sep 2, 2012, 9:04:03 PM9/2/12
to stan...@googlegroups.com
My understanding is that the out-of-range error on a std::vector triggered a SIGABRT. According to

https://en.wikipedia.org/wiki/SIGABRT

you can catch those. But I think the best can be done at that point is to call exit().

Ben

Bob Carpenter

unread,
Sep 3, 2012, 6:43:44 PM9/3/12
to stan...@googlegroups.com
We should be doing range testing within Stan itself.
We should then throw out of range ourselves. You can
check who through it by looking at the message.

- Bob

On 9/2/12 9:04 PM, Ben Goodrich wrote:
> My understanding is that the out-of-range error on a std::vector triggered a SIGABRT. According to
>
> https://en.wikipedia.org/wiki/SIGABRT
>
> you can catch those. But I think the best can be done at that point is to call exit().
>
> Ben
>
> On Sunday, September 2, 2012 8:51:24 PM UTC-4, Jiqiang Guo wrote:
>
> Please see comments below.
>
> Jiqiang
>
> --
>
>

Ben Goodrich

unread,
Sep 3, 2012, 8:04:07 PM9/3/12
to stan...@googlegroups.com
That sounds like a good idea, as long as it can be turned off with -DNDEBUG or whatever. I don't know how to do that for std::vectors though.

Ben

Bob Carpenter

unread,
Sep 4, 2012, 2:39:44 AM9/4/12
to stan...@googlegroups.com
Two things.

First, we should be turning off Eigen validation in
Stan in the makefiles using the -DEIGEN_NO_DEBUG option to
set the EIGEN_NO_DEBUG property.

We're already checking those ranges ourselves.

On 9/3/12 8:04 PM, Ben Goodrich wrote:
> That sounds like a good idea, as long as it can be turned off with -DNDEBUG or whatever. I don't know how to do that for
> std::vectors though.

Second, we just need to go through the getters
(I think in math/matrix) and apply the relevant
#ifdefs.

I think I've been putting it off because I'm
really opposed to preprocessor voodoo. But this
I can live with.

Just to confirm, I think the decision was to turn
off Stan range checking if either the NDEBUG or STAN_NO_DEBUG
property was defined (e.g., by using -DSTAN_NO_DEBUG as a compiler
argument).

I'll push that through unless anyone objects.

- Bob

Marcus Brubaker

unread,
Sep 4, 2012, 11:23:36 AM9/4/12
to stan...@googlegroups.com
That seems reasonable. Eigen uses EIGEN_NO_DEBUG everywhere in its
own source and then defines that when NDEBUG is defined to allow
selective debugging. (I.E., you can turn on EIGEN_NO_DEBUG without
turning on NDEBUG for everything.)

Cheers,
Marcus
> --
>
>
Reply all
Reply to author
Forward
0 new messages