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

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
主站蜘蛛池模板: 广丰县| 岳阳市| 永宁县| 肥西县| 建水县| 江门市| 温州市| 大埔区| 西林县| 镇江市| 万全县| 尖扎县| 海伦市| 鲜城| 周至县| 诸暨市| 监利县| 顺义区| 湖南省| 洪雅县| 乌恰县| 盘锦市| 闵行区| 宝坻区| 福鼎市| 都兰县| 博客| 五莲县| 弥渡县| 海宁市| 宿州市| 密山市| 黔西县| 兰溪市| 玉门市| 金秀| 宿州市| 长兴县| 怀宁县| 巫山县| 云安县|