#860 – Exceptions Bubble up the Call Stack
When you define a try/catch block, the handler defined in the catch block will catch exceptions that originate directly from the code in the try block. But the handler will also catch exceptions that...
View Article#861 – A finally Block Always Executes
When you use a try block, you’re defining a block of code for which you can catch an exception, using a catch block. You can also include a finally block, which dictates a block of code that will...
View Article#862 – Options for Including catch and finally Blocks
When you define a try block using the try keyword, you must also do one of the following: Include one or more catch blocks after the try block Include a finally block after the try block Include one or...
View Article#864 – An Example of a finally Block with No catch Block
When an exception occurs while executing code in a try block that has an associated finally block, the code in the finally block will execute before the exception bubbles up the call stack. Below is an...
View Article#865 – A catch Block Specifies the Type of Exception to Catch
In most cases, a catch block will specify the type of exception to intercept and catch. Any exception occurring in the associated try block whose type matches the type indicated in the catch block...
View Article#866 – A catch Block Without Arguments Catches All Exceptions
You typically include an argument in a catch block that indicates the type of exception to catch. catch (FileNotFoundException fnfExc) { Console.WriteLine(string.Format("Hey, we can't find file {0}!",...
View Article#867 – Including Several catch Blocks
You can include several catch blocks in a single try-catch statement, each catch block set up to catch a different kind of exception. try { DoSomething(5); } catch (FileNotFoundException noFileExc) {...
View Article#868 – List Most Specific Exception Types First
When you include more than one catch blocks as part of a try-catch statement, you must list the most specific exception types first. When an exception occurs, the type of the exception object is...
View Article#869 – Example of Catching an Exception Thrown by the .NET Framework
Below is a complete example that shows how we might catch an exception thrown by the .NET Framework. Our Dog class builds a list of all dogs created and allows you to retrieve a dog by index. public...
View Article#870 – Where Execution Continues when an Exception Is Caught
When an exception is thrown that you catch in a catch block, the code in the catch block will execute. Once all of the code in the catch block executes, execution will continue with the first line...
View Article#873 – Full Example of Throwing and Catching an Exception
Below is a complete example of throwing an exception from a method and catching/handling that exception further up the call stack. Code for Dog class that throws an exception if we ask a dog to bark...
View Article#880 – Catching Different Exception Types at Different Levels
You can include catch blocks at different points in the call stack, to catch different types of exceptions. In the example below, Main calls CreateSomeDogs, which in turn creates Dog instances and...
View Article#889 – Catching Exceptions that Derive from a Common Base Type
When you specify an exception type to catch in a handler, the handler will catch all exceptions whose type matches the specified type. It will also catch exceptions where the exception object’s type...
View Article#899 – Exception Type Variable in a catch Block Is Optional
In a catch block that specifies a particular exception type to catch, you’ll typically include a named variable representing the exception object that is caught. Using this variable, code within your...
View Article#911 – finally Block Execution When Exception Is Rethrown
Recall that if an exception is caught in a catch block, an associated finally block will execute after the execution of the catch block. If the catch block rethrows the exception, the finally block...
View Article