Game Development Reference
In-Depth Information
IDisposable interface, but that also means you can create custom classes that
require a cleanup process and use this keyword on them.
The following code shows how the using keyword works:
public void DoSomething(string fileName)
using (StreamReader reader = new StreamReader(string fileName))
This solution is much more elegant than an ugly exception handler, and still
ensures that the resource is released when it is no longer needed.
Unhandled and Thread Exception Events
There are a few issues when using either the AppDomain.UnhandledException event or
the Application.ThreadException event. The notification fires so late that by the
time you receive the exception notification, your application will be unable to
respond to it. Additionally, you will not receive any notifications if your exception
was thrown from the main thread or unmanaged code.
It is also very difficult to write a generic exception handler for the entire applica-
tion that is robust and flexible enough to accommodate and correctly handle every
erroneous situation. Because a generic handler would not have access to the local
variables present when the exception was thrown, the need to rely on global vari-
ables and singletons will be increased, which is something that should be ulti-
mately avoided.
With such faults, you are probably wondering why these events should be used in
the first place. Consider them as “safety nets” for the situations where an exception
slips through and would be normally handled by the default exception handler
provided by the Common Language Runtime.
Structured exception handling is and will remain an integral part of any software
project. This chapter covered some best practices for using .NET exception handling,
and it is highly advisable that you adopt these new techniques and approaches into
your development projects. Doing so will improve both the design and perfor-
mance of your code and will increase the overall maintainability of your software.
Search Nedrilad ::

Custom Search