官术网_书友最值得收藏!

Catching exceptions

In order to illustrate how exceptions bubble up the call stack, we are going to create a new averageAge function, which calculates the average age of a list of Person instances, using their string descriptions, as shown in the following code:

def averageAge(descriptions: Vector[String]): Double = {
val total = descriptions.map(createPerson).map(_.age).sum
total / descriptions.length
}

This function calls our previously implemented createPerson function, and therefore will throw any exception that is thrown by createPerson because there is no try...catch block in averageAge.

Now, we can implement another function on top that will parse an input containing several Person descriptions and return a summary in a string. It will print an error message in case the input cannot be parsed, as shown in the following code:

import scala.util.control.NonFatal
def
personsSummary(personsInput: String): String = {
val descriptions = personsInput.split("\n").toVector
val avg = try {
averageAge(descriptions)
} catch {
case e:AgeNegativeException =>
println(s"one of the persons has a negative age: $e")
0
case NonFatal(e) =>
println(s"something was wrong in the input: $e")
0
}
s"${descriptions.length} persons with an average age of $avg"
}

In this function, we declare an avg value, which will get the return value of averageAge if no exception is thrown. If one of the descriptions contains a negative age, our catch block will print an error message, and avg will be assigned the value of 0. If another type of exception is thrown, and this exception is NonFatal, then we print another message and avg will also be assigned the value of 0. A fatal exception is an exception that cannot be recovered, such as OutOfMemoryException. You can look at the implementation of scala.util.control.NonFatal for more details.

The following code shows a few sample calls to personsSummary:

scala> personsSummary(
"""John 25
|Sharleen 45""".stripMargin)
res1: String = 2 persons with an average age of 35.0

scala> personsSummary(
"""John 25
|Sharleen45""".stripMargin)
something was wrong in the input: java.lang.ArrayIndexOutOfBoundsException: 1
res2: String = 2 persons with an average age of 0.0

scala> personsSummary(
"""John -25
|Sharleen 45""".stripMargin)
one of the persons has a negative age: $line5.$read$$iw$$iw$AgeNegativeException: age should be > 0
res3: String = 2 persons with an average age of 0.0

As we can see, as soon as any of the descriptions cannot be parsed, an error is printed and the average age is set to 0.

主站蜘蛛池模板: 明星| 孟州市| 达日县| 芦溪县| 陈巴尔虎旗| 卢氏县| 迁西县| 沙坪坝区| 五华县| 东丽区| 咸宁市| 刚察县| 雷州市| 阿荣旗| 遵义市| 个旧市| 台东县| 墨玉县| 崇明县| 洪江市| 大埔区| 香河县| 富川| 福清市| 博罗县| 建始县| 仙游县| 孟连| 行唐县| 屏边| 铜陵市| 沂源县| 泰兴市| 军事| 澎湖县| 浏阳市| 榆树市| 丹棱县| 富顺县| 民权县| 巴彦淖尔市|