Information Technology Reference
In-Depth Information
polymorphically. Be on the lookout for any constructs that convert value
types to either System.Object or interface types: placing values in collec-
tions, calling methods defined in System.Object, and casts to System.Object.
Avoid these whenever you can.
Item 46: Create Complete Application-Specific Exception Classes
Exceptions are the mechanism of reporting errors that might be handled
at a location far removed from the location where the error occurred. All
the information about the error's cause must be contained in the excep-
tion object. Along the way, you might want to translate a low-level error to
more of an application-specific error, without losing any information
about the original error. You need to be very thoughtful about when you
create your own specific exception classes in your C# applications.
The first step is to understand when and why to create new exception
classes, and how to construct informative exception hierarchies. When
developers using your libraries write catch clauses, they differentiate
actions based on the specific runtime type of the exception. Each different
exception class can have a different set of actions taken:
try
{
Foo();
Bar();
}
catch ( MyFirstApplicationException e1)
{
FixProblem(e1);
}
catch ( AnotherApplicationException e2)
{
ReportErrorAndContinue(e2);
}
catch ( YetAnotherApplicationException e3)
{
ReportErrorAndShutdown(e3);
}
catch ( Exception e)
{
 
 
Search WWH ::




Custom Search