0

I wanted to know if there is a way to execute something after an error ocurred.

I am currently working with some databases and I need to liberate resources after an error, with a statement like dbDisconnect(database)

Thank you

Geiser
  • 1,054
  • 1
  • 12
  • 28

1 Answers1

1

Use a tryCatch with the finally block

tryCatch({
  # some code that initializes database
  # some code that runs a query
}, finally = dbDisconnect(database) )

The expression inside finally gets run regardless of whether the code in the try block was successful or threw an error.

DeanAttali
  • 25,268
  • 10
  • 92
  • 118