Game Development Reference
In-Depth Information
public NexusException(string message, Exception inner)
: base(String.Format(“{0} - {1}”, defaultMessage, message), inner)
{
}
}
Throwing Exceptions
A trait of exceptions is that they cannot be ignored, so it is a good idea to use
exceptions in place of return values when a particular operation must be success-
ful to proceed. It is also wise to rely more on throwing an exception than using
Debug.Assert . Keep in mind that assertions are removed from release code, so
errors will be much harder to track down in a production environment.
The stack trace is a critical piece of information to have when an exception is
thrown, and extra care must be taken when re-throwing exceptions to ensure that
the stack trace is preserved. Many times you need to catch a particular exception,
perform cleanup logic such as a transaction rollback, and then re-throw the excep-
tion so that another exception handler can process it.
Consider the following code:
try
{
// Code that throws an exception
}
catch (Exception exception)
{
// Code that performs cleanup \ rollback
throw exception;
}
In this example, the exception is caught and thrown back to the next exception
handler, but using a new exception object without the stack trace. The proper way
to re-throw the exception while preserving the stack trace information is shown in
the following code:
try
{
// Code that throws an exception
}
catch (Exception exception)
{
Search Nedrilad ::




Custom Search