ADBRITE ads links
You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Exception Handling
C# Cookbook, 2nd Edition
| Problem:
You have a method, property, or indexer that requires the correct value or set of values to be passed in to it (e.g., cannot be null, must be within a numeric range or a set of numeric ranges, the enumeration value must be a valid value in ...
|
|
| Problem:
You want to establish when it is appropriate to catch and rethrow an exception.
Solution:
It is appropriate if you have a section of code where you want to perform some action if an exception occurs, but not perform any actions to act...
|
|
| Problem:
There are many exceptions to choose from in the FCL. You need an easily accessible list of these exceptions that indicates when and where to use them. By throwing exceptions in a consistent manner (e.g., throwing an ArgumentNullException wh...
|
|
| Problem:
You have an exception hierarchy that consists of a base exception class and multiple derived exception classes. At some point in your code, you want to handle only one or two of these derived exceptions in a specific manner. All other deriv...
|
|
| Problem:
You have multiple nested try-catch, try-finally, and try-catch-finally blocks. If a catch block attempts to throw an exception, it is possible that the thrown exception will get discarded and that a new and unexpected exception will be caug...
|
|
| Problem:
Using reflection, you invoke a method that generates an exception. You want to obtain the real exception object and its information in order to diagnose and fix the problem.
Solution:
The real exception and its information can be obta...
|
|
| Problem:
You want to use a reflection-based technique, such as the static Assembly.LoadFrom method, to load an assembly. If this method fails, you want to collect as much useful information as you can as to why the assembly failed to load.
Solu...
|
|
| Problem:
You need a reference table that maps each managed exception to its COM hrESULT counterpart. This mapping will allow you to determine the relative mapping between managed exceptions and COM hrESULTs in unmanaged code.
Solution:
There c...
|
|
| Problem:
A COM object can return a user-defined hrESULT or an hrESULT that has no mapping to a managed exception type. You wish to handle these returned hrESULTs in a more specific manner.
Solution:
The following code fragment illustrates the ...
|
|
| Problem:
You need to make absolutely sure that every exception thrown by your application is handled and that no exception is bubbled up past the outermost exception handler. Hackers often use these types of exceptions to aid in their analysis of th...
|
|
| Problem:
There are several different methods of getting exception information. You need to choose the best one to use.
Solution:
The .NET platform supports several mechanisms for displaying exception information, depending on the specific type...
|
|
| Problem:
A thrown and caught exception can contain one or more inner exceptions. The innermost exception usually indicates the origin of the problem. You want to be able to view the original thrown exception and skip all of the outer exceptions and ...
|
|
| Problem:
None of the built-in exceptions in the .NET Framework provide the implementation details that you require for an exception that you need to throw. You need to create your own exception class that operates seamlessly with your application, a...
|
|
| Problem:
You need a view of what the stack looks like at any particular point in your application. However, you do not have an exception object from which to obtain this stack trace.
Solution:
Use the following line of code to obtain a stack t...
|
|
| Problem:
You need to fix a problem with your code that is throwing an exception. Unfortunately, an exception handler is trapping the exception, and you are having a tough time pinpointing where and when the exception is being thrown.
Forcing the ap...
|
|
| Problem:
Problem:s can occur when initializing a class's or a structure's static fields. Some of these problems are serious enough to raise a TypeInitializationException exception. Unfortunately, this exception can be hard to track down and can pote...
|
|
| Problem:
When using a delegate asynchronously, you want to be notified if the delegate has thrown any exceptions.
Solution:
Wrap the EndInvoke method of the delegate in a try/catch block:
using System;
using System.Threading;
public ...
|
|
| Problem:
You want to send some additional information along with an exception.
Solution:
Use the Data property on the System.Exception object to store key-value pairs of information relevant to the exception.
For example, say there is a Syste...
|
|
| Problem:
You want to see all of the exception data laid out differently from Visual Studio's presentation, as your exceptions usually have multiple inner exceptions showing the root cause.
Solution:
Create a debugger visualizer by deriving a c...
|
|
| Problem:
You have a WinForms-based application in which you want to catch and log any unhandled exceptions on any thread.
Solution:
You need to hook up handlers for both the System.Windows.Forms.Application. ThreadException event and the Syste...
|
|
You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Exception Handling
|
|
Related tags
Popular Categories
Unix books and guides
AJAX popular information
C# language guides
Windows books and cookbooks
.......
|
|