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

  • Clean Code in C#
  • Jason Alls
  • 342字
  • 2021-06-18 18:28:13

Exception Handling

In the previous chapter, we looked at functions. Despite the best efforts of programmers to write robust code, functions will, at some point, generate exceptions. This could be for a number of reasons, such as a missing file or folder, an empty or null value, the location can't be written to, or the user is denied access. So, with that in mind, in this chapter, you will learn about appropriate ways to use exception handling to produce clean C# code. First, we will start by looking at checked and unchecked exceptions with regards to arithmetic OverflowExceptions. We will look at what they are, why they are used, and some examples of them being used in code.

Then, we'll look at how we can avoid the NullPointerReference exception. After that, we'll look at implementing specific business rules for specific types of exceptions. With our fresh understanding of exceptions and exception business rules, we will set about building our own custom exceptions and then finish off by looking at why we should not use exceptions to control the flow of our computer programs.

In this chapter, we will cover the following topics:

  • Checked and unchecked exceptions
  • Avoiding NullPointerExceptions
  • Business rule exceptions
  • Exceptions should provide meaningful information
  • Building your own custom exceptions

By the end of this chapter, you will have the skills to do the following:

  • You will be able to understand what checked and unchecked exceptions are, and why they are in C#.
  • You will be able to understand what an OverflowException is and how to trap them at compile time.
  • You will know what NullPointerExceptions are and how to avoid them.
  • You will be able to write your own custom exceptions that provide meaningful information to the customer and that aid you and fellow programmers to easily identify and resolve any issues that are raised.
  • You will be able to understand why you should not use exceptions to control program flow.
  • You will know how to replace business rule exceptions with C# statements and Boolean checks to control program flow.
主站蜘蛛池模板: 盱眙县| 广南县| 文化| 鹿泉市| 岳池县| 岳阳市| 石景山区| 屯昌县| 墨玉县| 高州市| 黔南| 永德县| 沙河市| 丹阳市| 南澳县| 皮山县| 恭城| 昔阳县| 尉氏县| 江华| 溧水县| 武强县| 龙江县| 吴江市| 普兰店市| 东港市| 巴楚县| 麻江县| 射洪县| 色达县| 岳阳市| 五原县| 砚山县| 田林县| 睢宁县| 安塞县| 霍邱县| 乌鲁木齐市| 灯塔市| 东源县| 汾西县|