Skip to content

Instantly share code, notes, and snippets.

@symbolrush
Forked from Christoph999/debugging
Last active March 31, 2016 12:22
Show Gist options
  • Select an option

  • Save symbolrush/165057e08354aa7a041d575507f6a657 to your computer and use it in GitHub Desktop.

Select an option

Save symbolrush/165057e08354aa7a041d575507f6a657 to your computer and use it in GitHub Desktop.
# Error on purpose in line 31-36. Testscript for learning debugging using different options.
imsbasics::clc()
imsbasics::cc()
f1 <- function(a) {
# message("Message f1")
b <- 10
return(a + 1)
}
f2 <- function(a) {
# message("Message f2")
c <- 100
return(a + 2)
}
f3 <- function(a) {
# message("Message f3")
d <- 100
return(a + 20)
}
f4 <- function(a) {
# browser() # ............................... If you know where the error occurs
# message("Message f4")
const <- "hurz"
d <- 1000
# a <- f3(a)
if (a < 0) {
a = const
} else {
a = const
}
res <- a + 3
return(res)
}
old <- getOption("error")
# options(error = NULL)
# this returns:
# Fehler in a + 3 : nicht-numerisches Argument für binären Operator
# >
# # used traceback to figure out where in the call stack an error occurred
# options(error = traceback)
# returns:
# Fehler in a + 3 : nicht-numerisches Argument für binären Operator
# 6: f4(f3(f2(f1(3)))) at debugging.R#8
# 5: f1(f4(f3(f2(f1(3))))) at debugging.R#55
# 4: eval(expr, envir, enclos)
# 3: eval(ei, envir)
# 2: withVisible(eval(ei, envir))
# 1: source("~/Desktop/debugging.R")
# >
# browse the environment in the current function call. Not possible to step back / up in hierarchy
# options(error = browser)
# returns:
# Fehler in a + 3 : nicht-numerisches Argument für binären Operator
# Called from: f4(f3(f2(f1(3))))
# Browse[1]>
# Zusätzlich ist die Zeile im Source File highlighted.
# # use recover if you want to poke around in the environments for previous function calls.
# # In other words, you may want to \jump up to a higher level in the function call stack.
# options(error = recover)
c <- f1(f4(f3(f2(f1(3))))) # ... Not continued in case of error. Thus, d unknown!
# c <- try(f1(f4(f3(f2(f1(3)))))) # ... Continue in case of error. Thus, d = T!
# c <- tryCatch(f1(f4(f3(f2(f1(3))))), # ... Not continued in case of error. Thus, d unknown! But better error message!
# error = function(c) {
# c$message <- paste0(c$message, ": !!!your place for information!!!")
# stop(c)
# })
d <- T
options(error = old)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment