2  Message, Warning, Error

2.1 Conditions

We can handle warnings and errors with functions like try(), tryCatch() and withCallingHandlers()

Functions like suppressWarnings() and supressMessages() can control what messages you wish to see while using R.

Condition Explanation
message A generic notification/diagnostic message produced by the message() function; execution of the function continues.
warning An indication that something is wrong but not necessarily fatal; execution of the function continues. Warnings are generated by the warning() function.
error An indication that a fatal problem has occurred and execution of the function stops. Errors are produced by the stop() function.

2.2 Function correctly runs

  • All the following run of the function log is correct.
Code
log(10)
log(1)
log(0)
log(NA)

2.3 Function run shows warning

  • The following run of the function log will give warning()
Code
log(-5)
log(10, base = -1)

Warning message: In log(-5) : NaNs produced

Warning message: NaNs produced

2.4 Function runs shows error

  • The following run of the function log will give error

  • The functions often used to generate errors within a funciton are: stop(), stopifnot(), abort()

Code
log("A")
log(10, base = "e")
log(10, base = exp)

Error in log("A") : non-numeric argument to mathematical function

Error in log(10, base = "e") : non-numeric argument to mathematical function

Error in log(10, base = exp) : non-numeric argument to mathematical function

2.5 Function runs

  • Similar behaviour may be observed in Python function run

  • However, remember that the behaviour may change based on libraries used

Code
import math

# Correct
math.log(10)
math.log(1)
math.log2(10)

# Error
math.log(-5)
math.log(0)
math.log("A")

import numpy as np

# Correct
np.log(0)