Skip to content

Commit 838a405

Browse files
committed
use tryCatch with error = print around code that throws exceptions
this enables us to set the knitr error=FALSE chunk option to not allow articles with errors to render
1 parent dd7febe commit 838a405

6 files changed

+48
-40
lines changed

_posts/2013-01-13-intro-to-exceptions.md

+18-16
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ by a block of <code>try</code> and <code>catch</code>.
2222
A simple example will help.
2323

2424

25-
2625
{% highlight cpp %}
2726
#include <Rcpp.h>
2827

@@ -44,11 +43,11 @@ double takeLog(double val) {
4443
}
4544
{% endhighlight %}
4645

47-
4846
We can look at this example with a valid, and an invalid argument:
4947

5048
{% highlight r %}
51-
takeLog(exp(1)) # works
49+
# works
50+
takeLog(exp(1))
5251
{% endhighlight %}
5352

5453

@@ -60,16 +59,17 @@ We can look at this example with a valid, and an invalid argument:
6059

6160

6261
{% highlight r %}
63-
takeLog(-1.0) # throws exception
62+
# throws exception
63+
tryCatch(takeLog(-1.0),
64+
error = print)
6465
{% endhighlight %}
6566

6667

6768

6869
<pre class="output">
69-
Error: Inadmissible value
70+
&lt;std::range_error: Inadmissible value&gt;
7071
</pre>
7172

72-
7373
As we can see, execptions works as expected. By throwing an
7474
exception derived from the standard exception call, we arrive in
7575
the case first <code>catch</code> branch where the exception text
@@ -96,11 +96,11 @@ double takeLog2(double val) {
9696
}
9797
{% endhighlight %}
9898

99-
10099
Again, we can look at this example with a valid, and an invalid argument:
101100

102101
{% highlight r %}
103-
takeLog2(exp(1)) # works
102+
# works
103+
takeLog2(exp(1))
104104
{% endhighlight %}
105105

106106

@@ -112,16 +112,17 @@ Again, we can look at this example with a valid, and an invalid argument:
112112

113113

114114
{% highlight r %}
115-
takeLog2(-1.0) # throws exception
115+
# throws exception
116+
tryCatch(takeLog2(-1.0),
117+
error = print)
116118
{% endhighlight %}
117119

118120

119121

120122
<pre class="output">
121-
Error: Inadmissible value
123+
&lt;std::range_error: Inadmissible value&gt;
122124
</pre>
123125

124-
125126
This shows that due to the automatic addition of the needed
126127
infrastructure, exception handling can add a useful mechanism to
127128
signal error conditions back to R.
@@ -142,9 +143,9 @@ double takeLog3(double val) {
142143
}
143144
{% endhighlight %}
144145

145-
146146
{% highlight r %}
147-
takeLog3(exp(1)) # works
147+
# works
148+
takeLog3(exp(1))
148149
{% endhighlight %}
149150

150151

@@ -156,12 +157,13 @@ double takeLog3(double val) {
156157

157158

158159
{% highlight r %}
159-
takeLog3(-1.0) # throws exception
160+
# throws exception
161+
tryCatch(takeLog3(-1.0),
162+
error = print)
160163
{% endhighlight %}
161164

162165

163166

164167
<pre class="output">
165-
Error: Inadmissible value
168+
&lt;Rcpp::exception: Inadmissible value&gt;
166169
</pre>
167-

_posts/2013-04-08-rcpp-wrap-and-recurse.md

+1-5
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ List do_stuff( List x_ ) {
6464
}
6565
{% endhighlight %}
6666

67-
6867
A quick test:
6968

7069

@@ -86,7 +85,6 @@ print(tmp)
8685
[[2]]
8786
[1] 2 4 6 8 10
8887
</pre>
89-
9088

9189
Some notes on the above:
9290

@@ -108,7 +106,7 @@ We also check that we fail gracefully when we encounter a non-accepted `SEXP`:
108106

109107
{% highlight r %}
110108
tryCatch(do_stuff(list(new.env())),
111-
error = function(e) print(e))
109+
error = print)
112110
{% endhighlight %}
113111

114112

@@ -157,7 +155,6 @@ List recurse(List x_) {
157155
}
158156
{% endhighlight %}
159157

160-
161158
A test case:
162159

163160

@@ -191,7 +188,6 @@ $z$zy
191188
[1] 40
192189
</pre>
193190

194-
195191
Note that all we had to do was add a `VECSXP` case in our `switch` statement.
196192
If we see a list, we call the same `recurse` function on that list, and then
197193
re-assign the result of that recursive call. Neat!

_posts/2014-03-20-dynamic-dispatch-for-sparse-matrices.md

+7-11
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ arma::mat matmult_cpp(SEXP Xr, const arma::mat Y) {
5454
}
5555
{% endhighlight %}
5656

57-
5857
**Set up test cases:**
5958

6059
{% highlight r %}
@@ -82,20 +81,19 @@ X_ind <- as(sample(1:d, n, rep=TRUE), "indMatrix")
8281
Y <- matrix(1:(d*p), d, p)
8382
{% endhighlight %}
8483

85-
8684
**Check exception handling:**
8785

8886
{% highlight r %}
89-
matmult_cpp(as(X_ind, "ngTMatrix"), Y)
87+
tryCatch(matmult_cpp(as(X_ind, "ngTMatrix"), Y),
88+
error = print)
9089
{% endhighlight %}
9190

9291

9392

9493
<pre class="output">
95-
Error: unknown class of Xr
94+
&lt;Rcpp::exception: unknown class of Xr&gt;
9695
</pre>
9796

98-
9997
**Dense times dense:**
10098

10199
{% highlight r %}
@@ -150,11 +148,10 @@ benchmark(X_sp%*%Y,
150148

151149
<pre class="output">
152150
test replications elapsed relative
153-
2 matmult_cpp(X_sp, Y) 100 0.009 1.000
154-
1 X_sp %*% Y 100 0.025 2.778
151+
2 matmult_cpp(X_sp, Y) 100 0.006 1.000
152+
1 X_sp %*% Y 100 0.013 2.167
155153
</pre>
156154

157-
158155
**`indMatrix` times dense:**
159156

160157
{% highlight r %}
@@ -179,10 +176,9 @@ benchmark(X_ind%*%Y,
179176

180177
<pre class="output">
181178
test replications elapsed relative
182-
2 matmult_cpp(X_ind, Y) 100 0.013 1.000
183-
1 X_ind %*% Y 100 0.025 1.923
179+
2 matmult_cpp(X_ind, Y) 100 0.008 1.0
180+
1 X_ind %*% Y 100 0.012 1.5
184181
</pre>
185-
186182

187183
Based on [this](http://stackoverflow.com/a/22531129/295025) Q&A on StackOverflow,
188184
thanks again to Kevin Ushey for his helpful comment.

src/2013-01-13-intro-to-exceptions.cpp

+19-6
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ double takeLog(double val) {
4646
*/
4747

4848
/*** R
49-
takeLog(exp(1)) # works
50-
takeLog(-1.0) # throws exception
49+
# works
50+
takeLog(exp(1))
51+
52+
# throws exception
53+
tryCatch(takeLog(-1.0),
54+
error = print)
5155
*/
5256

5357
/**
@@ -81,8 +85,12 @@ double takeLog2(double val) {
8185
*/
8286

8387
/*** R
84-
takeLog2(exp(1)) # works
85-
takeLog2(-1.0) # throws exception
88+
# works
89+
takeLog2(exp(1))
90+
91+
# throws exception
92+
tryCatch(takeLog2(-1.0),
93+
error = print)
8694
*/
8795

8896
/**
@@ -106,6 +114,11 @@ double takeLog3(double val) {
106114
}
107115

108116
/*** R
109-
takeLog3(exp(1)) # works
110-
takeLog3(-1.0) # throws exception
117+
# works
118+
takeLog3(exp(1))
119+
120+
# throws exception
121+
tryCatch(takeLog3(-1.0),
122+
error = print)
111123
*/
124+

src/2013-04-08-rcpp-wrap-and-recurse.Rmd

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ We also check that we fail gracefully when we encounter a non-accepted `SEXP`:
9191

9292
```{r tidy=FALSE}
9393
tryCatch(do_stuff(list(new.env())),
94-
error = function(e) print(e))
94+
error = print)
9595
```
9696

9797
However, this only operates on top-level objects within the list. What if your

src/2014-03-20-dynamic-dispatch-for-sparse-matrices.Rmd

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ Y <- matrix(1:(d*p), d, p)
6868

6969
**Check exception handling:**
7070
```{r}
71-
matmult_cpp(as(X_ind, "ngTMatrix"), Y)
71+
tryCatch(matmult_cpp(as(X_ind, "ngTMatrix"), Y),
72+
error = print)
7273
```
7374

7475
**Dense times dense:**

0 commit comments

Comments
 (0)