| Line 1... |
Line 1... |
| 1 |
% File src/library/base/man/try.Rd
|
1 |
% File src/library/base/man/try.Rd
|
| 2 |
% Part of the R package, https://www.R-project.org
|
2 |
% Part of the R package, https://www.R-project.org
|
| 3 |
% Copyright 1995-2013 R Core Team
|
3 |
% Copyright 1995-2016 R Core Team
|
| 4 |
% Distributed under GPL 2 or later
|
4 |
% Distributed under GPL 2 or later
|
| 5 |
|
5 |
|
| 6 |
\name{try}
|
6 |
\name{try}
|
| 7 |
\alias{try}
|
7 |
\alias{try}
|
| 8 |
\title{Try an Expression Allowing Error Recovery}
|
8 |
\title{Try an Expression Allowing Error Recovery}
|
| 9 |
\description{
|
9 |
\description{
|
| 10 |
\code{try} is a wrapper to run an expression that might fail and allow
|
10 |
\code{try} is a wrapper to run an expression that might fail and allow
|
| 11 |
the user's code to handle error-recovery.
|
11 |
the user's code to handle error-recovery.
|
| 12 |
}
|
12 |
}
|
| 13 |
\usage{
|
13 |
\usage{
|
| 14 |
try(expr, silent = FALSE)
|
14 |
try(expr, silent = FALSE,
|
| - |
|
15 |
outFile = getOption("try.outFile", default = stderr()))
|
| 15 |
}
|
16 |
}
|
| 16 |
\arguments{
|
17 |
\arguments{
|
| 17 |
\item{expr}{an \R expression to try.}
|
18 |
\item{expr}{an \R expression to try.}
|
| 18 |
\item{silent}{logical: should the report of error messages be suppressed?}
|
19 |
\item{silent}{logical: should the report of error messages be
|
| - |
|
20 |
suppressed?}
|
| - |
|
21 |
\item{outFile}{a \link{connection}, or a character string naming the
|
| - |
|
22 |
file to print to (via \code{\link{cat}(*, file = outFile)});
|
| - |
|
23 |
used only if \code{silent} is false, as by default.}
|
| 19 |
}
|
24 |
}
|
| 20 |
\details{
|
25 |
\details{
|
| 21 |
\code{try} evaluates an expression and traps any errors that occur
|
26 |
\code{try} evaluates an expression and traps any errors that occur
|
| 22 |
during the evaluation. If an error occurs then the error
|
27 |
during the evaluation. If an error occurs then the error
|
| 23 |
message is printed to the \code{\link{stderr}} connection unless
|
28 |
message is printed to the \code{\link{stderr}} connection unless
|
| Line 29... |
Line 34... |
| 29 |
|
34 |
|
| 30 |
\code{try} is implemented using \code{\link{tryCatch}}; for
|
35 |
\code{try} is implemented using \code{\link{tryCatch}}; for
|
| 31 |
programming, instead of \code{try(expr, silent = TRUE)}, something like
|
36 |
programming, instead of \code{try(expr, silent = TRUE)}, something like
|
| 32 |
\code{tryCatch(expr, error = function(e) e)} (or other simple
|
37 |
\code{tryCatch(expr, error = function(e) e)} (or other simple
|
| 33 |
error handler functions) may be more efficient and flexible.
|
38 |
error handler functions) may be more efficient and flexible.
|
| - |
|
39 |
|
| - |
|
40 |
It may be useful to set the default for \code{outFile} to
|
| - |
|
41 |
\code{\link{stdout}()}, i.e., \preformatted{ options(try.outFile = stdout()) }
|
| - |
|
42 |
instead of the default \code{\link{stderr}()},
|
| - |
|
43 |
notably when \code{try()} is used inside a \code{\link{Sweave}} code
|
| - |
|
44 |
chunk and the error message should appear in the resulting document.
|
| 34 |
}
|
45 |
}
|
| 35 |
\value{
|
46 |
\value{
|
| 36 |
The value of the expression if \code{expr} is evaluated without error,
|
47 |
The value of the expression if \code{expr} is evaluated without error,
|
| 37 |
but an invisible object of class \code{"try-error"} containing the
|
48 |
but an invisible object of class \code{"try-error"} containing the
|
| 38 |
error message, and the error condition as the \code{"condition"}
|
49 |
error message, and the error condition as the \code{"condition"}
|