Chapter 13. Exception handling

If you are a Java developer, C#'s exception handling mechanism is very similar to Java's, and you will feel very comfortable. [1] There are, of course, new things to learn – C# has done away with checked exceptions and built in something called inner exceptions, as well as other minor improvements.

[1] VB 6 developers will find exception handling a totally new thing to pick up when they move over to VB .NET. VB 6 has no proper exception handling mechanism, and developers rely on the ancient On Error Goto construct to handle errors.

C# has four keywords for exception handling – try, catch, finally, and throw. Notice that C# does not have the throws keyword which Java developers use when declaring methods.

I shall start with a general example of exception handling in C# just to warm up a bit. This will be followed by examples on nested try blocks and user-defined exceptions. [2] Then I will discuss the following topics:

[2] User-defined exceptions are exception classes written by the developer.

  • The C# exception class hierarchy;

  • System.Exception class;

  • inner exceptions;

  • general catch block;

  • catching generic exceptions.

..................Content has been hidden....................

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