@@ -17,13 +17,15 @@ import scala.runtime.Statics
17
17
import scala .util .control .NonFatal
18
18
19
19
/**
20
- * The `Try` type represents a computation that may either result in an exception, or return a
21
- * successfully computed value. It's similar to, but semantically different from the [[scala.util.Either ]] type.
20
+ * The `Try` type represents a computation that may fail during evaluation by raising an exception.
21
+ * It holds either a successfully computed value or the exception that was thrown.
22
+ * This is similar to the [[scala.util.Either ]] type, but with different semantics.
22
23
*
23
- * Instances of `Try[T]`, are either an instance of [[scala.util.Success ]][T] or [[scala.util.Failure ]][T].
24
+ * Instances of `Try[T]` are an instance of either [[scala.util.Success ]][T] or [[scala.util.Failure ]][T].
24
25
*
25
- * For example, `Try` can be used to perform division on a user-defined input, without the need to do explicit
26
- * exception-handling in all of the places that an exception might occur.
26
+ * For example, consider a computation that performs division on user-defined input.
27
+ * `Try` can reduce or eliminate the need for explicit exception handling in all of the places
28
+ * where an exception might be thrown.
27
29
*
28
30
* Example:
29
31
* {{{
@@ -58,8 +60,6 @@ import scala.util.control.NonFatal
58
60
* Serious system errors, on the other hand, will be thrown.
59
61
*
60
62
* ''Note:'': all Try combinators will catch exceptions and return failure unless otherwise specified in the documentation.
61
- *
62
- * `Try` comes to the Scala standard library after years of use as an integral part of Twitter's stack.
63
63
*/
64
64
sealed abstract class Try [+ T ] extends Product with Serializable {
65
65
@@ -202,13 +202,22 @@ sealed abstract class Try[+T] extends Product with Serializable {
202
202
}
203
203
204
204
object Try {
205
- /** Constructs a `Try` using the by-name parameter. This
206
- * method will ensure any non-fatal exception is caught and a
207
- * `Failure` object is returned.
205
+ /** Constructs a `Try` using the by-name parameter as a result value.
206
+ *
207
+ * The evaluation of `r` is attempted once.
208
+ *
209
+ * Any non-fatal exception is caught and results in a `Failure`
210
+ * that holds the exception.
211
+ *
212
+ * @param r the result value to compute
213
+ * @return the result of evaluating the value, as a `Success` or `Failure`
208
214
*/
209
215
def apply [T ](r : => T ): Try [T ] =
210
- try Success (r) catch {
211
- case NonFatal (e) => Failure (e)
216
+ try {
217
+ val r1 = r
218
+ Success (r1)
219
+ } catch {
220
+ case NonFatal (e) => return Failure (e)
212
221
}
213
222
}
214
223
@@ -247,7 +256,6 @@ final case class Failure[+T](exception: Throwable) extends Try[T] {
247
256
override def fold [U ](fa : Throwable => U , fb : T => U ): U = fa(exception)
248
257
}
249
258
250
-
251
259
final case class Success [+ T ](value : T ) extends Try [T ] {
252
260
override def isFailure : Boolean = false
253
261
override def isSuccess : Boolean = true
0 commit comments