Skip to content
Prev 305815 / 398506 Next

Trap an error from a function

On Sep 18, 2012, at 5:10 PM, John Sorkin wrote:

            
?conditions   #### has lots of fancy stuff
# But I use just plain old `try`

test=-10:10
sapply(test, function(x)  if( "try-error" %in% 
                     class( try( test[test[x:1]] ) ) ){
                     2}else{0} )
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
Error in test[x:1] : only 0's may be mixed with negative subscripts
 [1] 2 2 2 2 2 2 2 2 2 2 0 0 0 0 0 0 0 0 0 0 0

You still get the messages but the code runs.