R’s tryCatch function is a great tool that helps facilitate robust error handling. It lets you try to run a block of code and if an error occurs, the catch part of the function can be used to handle exceptions in a customized manner (as opposed to halting the entire script). I have personally been deploying this design pattern pretty regularly and there are two situations in which I’ve found tryCatch to be an especially handy tool in my toolbox:
During a tutorial I gave for the University of Guelph R users group, we were going through how to generate summary stats & tidy dataframes from messy data sources. This involved working with text data, and the exercise called for us to process a series of sentences and answer 3 questions about each line:
Is the line dialogue? (presence of a quotation mark in the string) Is the line a question?