added Rcpp exception handling to avoid segfault caused by wrong input types.