Try, Success, Failure

Question Click to View Answer

What does the following code return?

new java.net.URL("this is invalid")

java.net.MalformedURLException: no protocol: this is invalid is returned.

The java.net.URL constructor throws an error when the input string is an invalid URL.

What does the following code return?

val u = new java.net.URL("https://mungingdata.com/")
println(u.getProtocol)

https is printed.

The input supplied to the constructor is valid, so a java.net.URL object is created.

Java libraries are easily accessible in Scala code.

What does the following code return?

import scala.util.Try

Try(new java.net.URL("this is invalid"))

Failure(java.net.MalformedURLException: no protocol: this is invalid) is returned.

Failure is a Scala class that lets errors be handled in a functional way. Notice that the program does not blow up with an exception when new java.net.URL("this is invalid") is wrapped in Try.

Try lets you deal with an error like any other value, so it can be handled with functional code.

What does the following code return?

import scala.util.Try

Try(new java.net.URL("https://www.mungingdata.com"))

Success(https://www.mungingdata.com) is returned.

Success is a Scala class that is returned when the argument passed to Try does not throw an exception.

new java.net.URL("https://www.mungingdata.com") does not throw an exception, so a Success is returned.

What does the following code print?

val url = Try(new java.net.URL("https://www.blah.com"))
val res = if (url.isSuccess) "we have a URL" else "where to go?"
println(res)

we have a URL is printed.

You can call isSuccess on instances of the Try class.

There is also an isFailure method.

What does the following code print?

val url = Try(new java.net.URL("blah"))
println(url.getOrElse("I am broken"))

I am broken is printed.

The getOrElse method can be called on Success and Failure objects.

This allows for Try values to be handled in a functional way.

What does the following code print?

val url = Try(new java.net.URL("blah"))
println(url.map(_.getProtocol))

Failure(java.net.MalformedURLException: no protocol: blah) is returned.

The URL cannot be parsed and we're calling the getProtocol method on a value that we couldn't parse.

If we weren't using Scala's functional error handling, this code would error out.

Scala wraps the error in a return value, so the code does not blow up and the error value can be processed in a functional manner.

What does the following code print?

val url = Try(new java.net.URL("https://www.mungingdata.com"))
println(url.map(_.getProtocol))

Success(https) is printed.

The return type is scala.util.Try[String].

The URL is successfully parsed in this example and we can grab the protocol in a functional manner.

What does the following code print?

def toInt(s: String): Try[Int] =
    Try(Integer.parseInt(s.trim))

println(toInt("80"))

Success(80) is printed.

The toInt function takes a string argument and returns a Try[Int] value. The string "80" can be parsed to an integer, so a Success value is returned.

What does the following code print?

def toInt(s: String): Try[Int] =
    Try(Integer.parseInt(s.trim))

println(toInt("hello"))

Failure(java.lang.NumberFormatException: For input string: "hello") is printed.

The string "hello" cannot be parsed into an integer.

What does the following code print?

def toInt(s: String): Option[Int] = {
  try {
    Some(Integer.parseInt(s.trim))
  } catch {
    case e: Exception => None
  }
}

println(toInt("55"))
println(toInt("hello"))

toInt("55") returns Some(55).

toInt("hello") returns None.

We can deal with exceptions in a functional manner with Option / Some / None, but it's awkward.

It's better to deal with exceptions in a functional manner using Try / Success / Failure.