Skip to content

When something fails

You may want to create a breakpoint when an error occurs.

In Python, you can use a try statement to catch exceptions and create a breakpoint:

1
2
3
4
5
6
7
8
def print_c(data):
    print(data['c'])

x = {'a': 1, 'b': 2}
try:
    print_c(x)
except KeyError:
    breakpoint()

In R, you can take a similar approach and use tryCatch():

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
f <- function(x) {
  if (x == 10) { stop("ten") }
  x
}

g <- function(x_max) {
  result <- tryCatch(
    sapply(seq_len(x_max), f),
    error = function(e) NULL
  )
  if (is.null(result)) {
    browser()
  }
  result
}

g(9)  # This completes without an error.
g(12) # This triggers the breakpoint.

In R, you can also define a global error handler (e.g., browser or recover):

19
20
21
options(error=recover)

g(12)