10.6. Summary

The .NET CLR supports exception handling in the form of a Java-style try-catch-finally construct. Here are the key points to keep in mind about C# exception handling:

  • A try block must be followed by one or more catch blocks or by a finally block, or both.

  • The most general exception needs to be caught in the last catch block.

  • A try-catch block can be nested. Exceptions bubble up the execution stack to outer try-catch blocks. Any uncaught exceptions bubble up the method call stack until they are handled by the CLR.

  • Like Java, C# has one System.Exception class from which all other exception classes inherit.

  • C# does not support checked exceptions, unchecked exceptions, or errors.

  • C# allows implementing classes to throw exceptions that are not explicitly declared in their corresponding interface methods. Also, subclasses can override superclass methods and throw new exceptions in the overridden methods.

  • In C# you can create custom exceptions, which allow more control over how expressive your exceptions can be.

  • There are four ways to deal with exceptions: Do nothing about a thrown exception (often the best approach); clean up and reset the object's state and throw the same exception; clean up and reset the state and rethrow the exception as a custom exception; or handle the exception.

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

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