@@ -22,7 +22,6 @@ by a block of <code>try</code> and <code>catch</code>.
22
22
A simple example will help.
23
23
24
24
25
-
26
25
{% highlight cpp %}
27
26
#include <Rcpp.h>
28
27
@@ -44,11 +43,11 @@ double takeLog(double val) {
44
43
}
45
44
{% endhighlight %}
46
45
47
-
48
46
We can look at this example with a valid, and an invalid argument:
49
47
50
48
{% highlight r %}
51
- takeLog(exp(1)) # works
49
+ # works
50
+ takeLog(exp(1))
52
51
{% endhighlight %}
53
52
54
53
@@ -60,16 +59,17 @@ We can look at this example with a valid, and an invalid argument:
60
59
61
60
62
61
{% highlight r %}
63
- takeLog(-1.0) # throws exception
62
+ # throws exception
63
+ tryCatch(takeLog(-1.0),
64
+ error = print)
64
65
{% endhighlight %}
65
66
66
67
67
68
68
69
<pre class =" output " >
69
- Error: Inadmissible value
70
+ & lt ; std::range_error: Inadmissible value& gt ;
70
71
</pre >
71
72
72
-
73
73
As we can see, execptions works as expected. By throwing an
74
74
exception derived from the standard exception call, we arrive in
75
75
the case first <code >catch</code > branch where the exception text
@@ -96,11 +96,11 @@ double takeLog2(double val) {
96
96
}
97
97
{% endhighlight %}
98
98
99
-
100
99
Again, we can look at this example with a valid, and an invalid argument:
101
100
102
101
{% highlight r %}
103
- takeLog2(exp(1)) # works
102
+ # works
103
+ takeLog2(exp(1))
104
104
{% endhighlight %}
105
105
106
106
@@ -112,16 +112,17 @@ Again, we can look at this example with a valid, and an invalid argument:
112
112
113
113
114
114
{% highlight r %}
115
- takeLog2(-1.0) # throws exception
115
+ # throws exception
116
+ tryCatch(takeLog2(-1.0),
117
+ error = print)
116
118
{% endhighlight %}
117
119
118
120
119
121
120
122
<pre class =" output " >
121
- Error: Inadmissible value
123
+ & lt ; std::range_error: Inadmissible value& gt ;
122
124
</pre >
123
125
124
-
125
126
This shows that due to the automatic addition of the needed
126
127
infrastructure, exception handling can add a useful mechanism to
127
128
signal error conditions back to R.
@@ -142,9 +143,9 @@ double takeLog3(double val) {
142
143
}
143
144
{% endhighlight %}
144
145
145
-
146
146
{% highlight r %}
147
- takeLog3(exp(1)) # works
147
+ # works
148
+ takeLog3(exp(1))
148
149
{% endhighlight %}
149
150
150
151
@@ -156,12 +157,13 @@ double takeLog3(double val) {
156
157
157
158
158
159
{% highlight r %}
159
- takeLog3(-1.0) # throws exception
160
+ # throws exception
161
+ tryCatch(takeLog3(-1.0),
162
+ error = print)
160
163
{% endhighlight %}
161
164
162
165
163
166
164
167
<pre class =" output " >
165
- Error: Inadmissible value
168
+ & lt ; Rcpp::exception: Inadmissible value& gt ;
166
169
</pre >
167
-
0 commit comments