Exercise 17: Try Catch
Explore how to implement error handling in R using try and catch. This lesson helps you create functions that manage exceptions gracefully, such as preventing failures when calculating logarithms of invalid inputs, ensuring your code runs smoothly.
We'll cover the following...
We'll cover the following...
Problem Statement
Implement a function calculateLog() that takes a testVariable and prints its log. If the logarithmic value of the testVariable is not possible, print “This operation is not allowed!”
Use the
cat()function for printing!
Input
A testVariable containing the data whose logarithmic value needs to be found.
Output
The correct output based on the testVariable.
Sample Input
"a"
Sample Output
This operation is not allowed!
Test Yourself
Write your code in the given area. If you get stuck, you can look at the solution.
In the next lesson, we have a solution review of this exercise.