#863 – How a finally Block Works with No catch Block
When you include a finally block with some code that’s surrounded by a try block, you’re saying that you want the code in the finally block to run after the code in the try block, whether or not an...
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#871 – Where Execution Continues when an Exception Is Not Caught
When an exception is not caught by a try-catch statement, the code that follows the try-catch statement will not execute. In the example below, Main() calls MethodA, which calls MethodB. MethodB...
View Article#872 – Code After a throw Statement Is Not Executed
When you throw an exception using the throw statement, control transfers immediately to the calling function. No additional statements in the method containing the throw statement are executed. In the...
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#874 – An Exception Can Be Thrown from a Constructor
You can throw an exception from a constructor. For example, in the code below, the Dog constructor throws an exception if an invalid age parameter is passed in. // Dog constructor public Dog(string...
View Article#875 – Looking at the Call Stack after Catching an Exception
After an exception is thrown, it bubbles up the call stack until a handler that can handle the exception is found. If you set a breakpoint in an exception handler, you can then use the debugger in...
View Article#876 – Unhandled Exceptions
If an exception is thrown while your code is executing and you don’t have a handler that catches the exception, it is considered an unhandled exception. An unhandled exception is one that travels all...
View Article#878 – Unhandled Exceptions in Static Constructors
If any exception is thrown from within a static constructor, a TypeInitializationException will be thrown, with an InnerException set to the original exception that occurred. If this exception is not...
View Article#879 – Unhandled Exceptions in Finalizers
Like other unhandled exceptions, an exception that is thrown from a finalizer and not handled will cause your application to terminate. ~Dog() { Console.WriteLine( "Dog finalizer is running. I'll...
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#881 – When to Throw Exceptions
Your code should throw an exception when Something has gone wrong and your code cannot execute normally You catch an exception and want to add some additional information You catch an exception and...
View Article#882 – What Types of Exceptions to Throw
When you throw an exception from your code, you should throw one of the following exception types: One of the preexisting exception types, if the error matches the exception type (e.g....
View Article#883 – Re-throwing an Exception
When you catch an exception in an exception handler, you have the option of executing some code and then re-throwing the exception. When you re-throw an exception from a handler, code that called the...
View Article
More Pages to Explore .....