Skip to content
Prev 303220 / 398503 Next

Stopping all code execution when ANY error occurs (OR error handling without try/tryCatch)

Hi Phil,

I really have to concur with Uwe Ligges here. You probably want to wrap 
everything in a function. Because usually this is where long lines of 
code will end up.

The browser() function makes this option really useful, as you can step 
inside the temporary environment and work from there as if you are in 
the global environment with the added benefits that all your variables 
will be gone when you exit the execution, so you start with a new blank 
environment the next time you call the function. And if you put the 
browser call to the botton of your functions, any error before will halt 
execution.

The downsites are, that you need to execute the function code each time 
you make changes to the function prior to rerunning it. But IDEs will 
help on this by sourcing a function qith simple keystrokes (e.g., C-c 
C-f in ESS-Emacs). However, if there is a syntax error, the original 
problem remains.

An alternative to browser() is to use options(error = recover) when 
working with functions. It is also extremely helpful, as it brings you 
to the point of the function where the error occurs to work from there. 
Give it a try.
BTW, I got the info with browser(error = recover) from the Chambers 
book: Sofwtare for Data analysis. It is worth a buy.

Cheers,
Henrik

enocko schrieb: