Re-Throwing Exceptions
When an exception is caught in a catch block, the exception is considered to have been processed and code execution continues as normal. In some situations it is useful to catch the exception but still have it thrown to be caught again. For example, a method may catch all exceptions so that they can be logged and then re-throw the exception to be handled appropriately by the calling function.
When an exception is thrown explicitly using the syntax previously discussed, a new exception object is constructed. This exception object contains only the basic information set by the programmer; information such as the StackTrace property of the original exception is lost. To re-throw the exception and retain this additional information, the throw command is used without specifying an exception object. This is achieved using the following syntax:
The following example code demonstrates how this syntax can be used. For simplicity, the methods called are not defined so this code cannot be directly executed.
C# Interview Questions & Answers, Part 2
11. How can you sort the elements of the array in descending order?
By calling Sort() and then Reverse() methods.
12. What’s the .NET collection class that allows an element to be accessed using a unique key?
HashTable.
13. What class is underneath the SortedList class?
A sorted HashTable.
14. Will the finally block get executed if an exception has not occurred?
Yes.
15. What’s the C# syntax to catch any possible exception?
A catch block that catches the exception of type System.Exception. You can also omit the parameter data type in this case and just write catch {}.
16. Can multiple catch blocks be executed for a single try statement?
No. Once the proper catch block processed, control is transferred to the finally block (if there are any).
17. Explain the three services model commonly know as a three-tier application.
Presentation (UI), Business (logic and underlying code) and Data (from storage or other sources).