Exception Handling

Let's begin this chapter by looking at two words: exception and handling. In English, the word exception refers to something unusual that doesn't usually happen. In programming, the word exception has a similar meaning, but is related to software code. By their nature, computer programs should do only those things that we instruct them to do, and it is considered abnormal when a computer won't or can't follow our instructions. If the computer program fails to follow our instructions, it is classified as an exception in the software world.

Error is another word that is heavily used in programming. It is important for us to understand that an error and an exception are not the same thing. An error refers to an incident where the software couldn't even run. More specifically, an error means that the code that is written contains something wrong, and that is why the compiler couldn't compile/build the code. On the other hand, an exception is something that happens at runtime. The easiest way to distinguish between these two concepts is—if the code doesn't compile/build, then there is an error in your code. If the code compiles/builds, but when you run it you get some unusual behavior, then it's an exception.

Exception handling means handling/controlling/supervising exceptions that occur while we are running the program. The topics that we are going to explore in this chapter are as follows:

  • Why we need exception handling in programming
  • Exception handling in C# programming
  • The basics of exception handling
  • try and catch
  • What happens if you don't handle exceptions
  • Multiple catch blocks
  • What the throw keyword is used for
  • What the finally block does
  • Exception classes
  • Some common exception classes
  • Exception handling best practices
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.185.87