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

Handling Errors

In this chapter, we will continue working on the retirement calculator that we implemented in Chapter 2, Developing a Retirement Calculator. Our calculator worked correctly as long as we passed the right arguments, but would fail badly with a horrible stack trace if any of the parameters were wrong. Our program only worked for what we call the happy path.

The reality of writing production software is that all kinds of error scenarios can occur. Some of them are recoverable, some of them must be presented to the user in an attractive way, and, for some hardware-related errors, we might need to let the program crash.

In this chapter, we will introduce exception handling, explain what referential transparency is, and try to convince you that exceptions are not the best way to deal with errors. Then, we will explain how to use functional programming constructs to effectively handle the possibility of an error.

In each section, we will briefly introduce a new concept, and then use it in a Scala worksheet to get a sense of how to use it. After that, we will apply this new knowledge to improve the retirement calculator.

In this chapter, we will look at the following topics:

  • Using exceptions when necessary
  • Understanding referential transparency
  • Using Option to represent optional values
  • Using Either to handle errors sequentially
  • Using Validated to handle errors in parallel
主站蜘蛛池模板: 河南省| 云阳县| 三穗县| 炎陵县| 卢龙县| 仙游县| 潞西市| 松江区| 偃师市| 岗巴县| 泽州县| 佛教| 伽师县| 翁牛特旗| 宁都县| 澄江县| 米易县| 铜梁县| 黄冈市| 佛冈县| 绥阳县| 五华县| 益阳市| 肥东县| 扎兰屯市| 江达县| 乐陵市| 独山县| 信丰县| 遂溪县| 长汀县| 隆德县| 资阳市| 河北省| 宝丰县| 隆子县| 陵水| 莒南县| 内江市| 凤翔县| 道真|