Solution Review: Try Catch
Explore how to apply the tryCatch block in R to manage errors and warnings when running functions such as log. Learn to ensure your code handles exceptions properly and creates more robust scripts.
We'll cover the following...
We'll cover the following...
Solution: Try Catch Block
Explanation
We use
log()to calculate the log of a variable or constant.
Here, log(test) is the function call which may throw an exception, therefore we have used tryCatch() to handle errors and warnings.
We have to write both the error and warning functions because for negative numbers the log() function throws a warning and for non-integers, it throws an error.
In the next lesson, we will be learning other methods for exception handling.