0

I am currently working on an R algorithm that predicts the chances of a team winning the NBA finals based on possible acquisitions of certain players. A part of the program is dependent on user input (which team, which player gets signed, etc), and I wanted to create fail-safes in case the user inputted invalid arguments. I have experience coding and throwing exceptions in Java, but was wondering what would be the best way to throw them in R? Specifically, I was looking at how to throw exceptions comparable to the "RunTimeException" and "IndexOutOfBoundsException" as seen in Java.

Vib
  • 1
  • Look into `tryCatch`? – CPak Apr 23 '18 at 20:58
  • Throw: `stop`. Catch: `tryCatch` or `withCallingHandlers` with `withRestarts`. Unfortunately, errors in R are almost completely textual, meaning you need to `grep` for specific errors. There are some packages that provide error types, but they are (I think) the exception (pun intended). Some other related questions: https://stackoverflow.com/questions/20572288/capture-arbitrary-conditions-with-withcallinghandlers and https://stackoverflow.com/questions/32167959/why-does-withcallinghandlers-still-stops-execution. – r2evans Apr 23 '18 at 21:04
  • A great resource is Hadley Wickham's Advanced R chapter on exceptions here http://adv-r.had.co.nz/Exceptions-Debugging.html – GordonShumway Apr 23 '18 at 21:44
  • It only gets a brief mention in the above resource but the `assertthat` package allows you to write more explicit error messages when inputs or any other object inside your function aren't of the type that you expect them to be. – Calum You Apr 24 '18 at 00:03
  • @r2evans thank you I will implement these into my code! – Vib Apr 24 '18 at 00:51
  • @GordonShumway I will give it a look, thanks for the reference – Vib Apr 24 '18 at 00:52
  • @CalumYou I can use assertthat multiple times in my code then thanks! – Vib Apr 24 '18 at 00:53

0 Answers0