Game Development Reference
In-Depth Information
These constructors are:
const string defaultMessage = “Your default message”;
public YourException()
: base(defaultMessage);
public YourException(string message)
: base(String.Format(“{0} - {1}”, defaultMessage, message));
public YourException(string message, Exception inner)
: base(String.Format(“{0} - {1}”, defaultMessage, message), inner);
There are some rules that must be followed in regards to the message property. Do
not store exception information in the message property. Instead, create separate
properties in the exception class to hold the data. Storing information in the mes-
sage property also means that users will have to perform string parsing to retrieve
the data, which is an obvious hassle. Also consider the problems that localization
would present if you were attempting to parse different pieces of information from
the exception message when the formatting of the string was based on the current
culture locale.
Lastly, be sure to mark your exceptions with the [Serializable] attribute. You
never know when your methods will be called from Remoting or a Web Service.
Here is a complete example of a custom exception in its simplest form:
[Serializable]
public class NexusException : System.Exception
{
const string defaultMessage = “A runtime error occurred within” +
“Nexus World Builder”;
public NexusException()
: base(defaultMessage)
{
}
public NexusException(string message)
: base(String.Format(“{0} - {1}”, defaultMessage, message))
{
}
Search Nedrilad ::




Custom Search