Skip to content

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

7 messages · Bert Gunter, Uwe Ligges, enocko +3 more

#
Hi all,

I often run a bunch of code with Run Selection, and, after it's done
running, I find there have been like 20 errors, all due to an error that
occurred early in my code, which caused problems from there on. (I must then
scroll up through lots of code to find it.)

What I would like is for R to stop executing when an error occurs.

I am aware of a few solutions:
1. Put all the code in a function
2. Put all the code in a tryCatch statement with error handling
3. *partial solution only* Replace stop() with my own function, so at least
if the errors are coming from a call to stop, I can catch them and halt all
code execution. (I do this halting with a hack involving quit() and .Last(),
so also please let me know if there's a better way)

However, I don't want to put all the code in braces while I'm just
developing/playing around with code. I run various snippets of code all the
time, and it's time consuming to always make a tryCatch or function
surrounding whatever I'm executing.

Is there any way to do a kind of global error handling, so I could source()
some file once and then have it set for the rest of my session that day?

To see multiple errors that R doesn't stop, you can put these 2 lines into a
script and do Run Selection:
5[[2]] <- 1
list() <- 1

You'll notice that 2 errors occur, whereas what I want is for execution to
just stop after the first error, so that the second line never gets run.

Thanks very much,

Phil



--
View this message in context: http://r.789695.n4.nabble.com/Stopping-all-code-execution-when-ANY-error-occurs-OR-error-handling-without-try-tryCatch-tp4640023.html
Sent from the R help mailing list archive at Nabble.com.
#
1. You probably need to tell us on what platform (OS, version) you are
running, as requested by the posting guide.

2. To me, it sounds like your question is: How can I avoid using the
error trapping tools built into R but still trap errors. If so, pretty
nonsensical, no?

3. But maybe someone else can suggest something useful. My thought is
that what you really need is to think out of the box and use a better
IDE. Check out CRAN for alternatives, but ESS, RStudio, Tinn-R, and
WInedit immediately come to mind. what will work for you depends on
your platform (ergo 1 above).

Cheers,
Bert
On Sat, Aug 11, 2012 at 1:07 AM, enocko <enotrash at gmail.com> wrote:

  
    
#
Easy: Wrap your code into a function. A function by default exits on the 
first error.

Best,
Uwe Ligges
On 11.08.2012 10:07, enocko wrote:
#
Hi, thanks for the ideas, folks.

I'm on Windows 7, R 2.15.0 x64, RStudio 0.97.71.

I do appreciate your time... I would like to say my goal of dealing with
errors without R's error trapping tools is not nonsensical given that those
tools are cumbersome and not well-suited to the development phase of coding
where one informally runs various snippets all the time.

The suggestion of looking at IDE's is a good one because it would not be
hard for an IDE to just wait and see if any line of code gives an error, and
halt execution if so (a global option could enable this). RStudio doesn't
have this--does anyone know of something that does? I can post a suggestion
to RStudio.

Thanks,

Phil




--
View this message in context: http://r.789695.n4.nabble.com/Stopping-all-code-execution-when-ANY-error-occurs-OR-error-handling-without-try-tryCatch-tp4640023p4640073.html
Sent from the R help mailing list archive at Nabble.com.
#
Hello,

Something like this?


x <- 1:5  # vector with more than one element
# warning, execution continues
if(x == 2) print(2) else print("No!")

ow <- options(warn = 2) # all warnings are now errors
# error, else clause not executed
if(x == 2) print(2) else print("No!")
options(ow)  # reset default warn level


(Another way is the well known print function plus wait for execution to 
stop.)

Hope this helps,

Rui Barradas

Em 12-08-2012 02:56, enocko escreveu:
#
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:

  
    
#
In RStudio use
    File > New > R Script
to make a script window (and file) and run it using
    Code > Source
(or Control-Shift-S).  The script will stop at the first error.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com