ExceptionHelper Class
Provides utility methods to construct exception messages.
public static class ExceptionHelperMethods
| Name | Description |
|---|---|
| FormatObject(object?, string?) | Returns a text representation of an object. |
FormatObject(object?, string?)
Returns a text representation of an object.
Parameters
objobject?- The object to represent. Can be
null. formatstring?- An optional format string to apply if
objimplementsIFormattable.
Returns
string A text representation of obj.
Remarks
This method attempts to never throw exceptions and always return meaningful text, without cluttering exception messages with potentially very long string values, by using the following algorithm:
- if
objisnull, returnExceptionHelper.NullText; - if
objis astring, convert it to a quoted string literal, clipping it if longer than 43 characters by leaving the first and last 20 characters separated by an ellipsis (three dots); - otherwise, check whether
objimplementsIFormattableand act accordingly.
If obj implements IFormattable:
- try to format
objusing the specifiedformatand the invariant culture; - if an exception is thrown, try again using an empty format string;
- if another exception is thrown, treat
objas if it didn't implementIFormattable.
If obj does not implement IFormattable:
- call
obj.ToString(); - if
ToStringthrows an exception, return a string of the form<{objType}:{exceptionType}>; - if
ToStringreturnsnull, returnExceptionHelper.ToStringNullText; - if
ToStringreturns the empty string, returnExceptionHelper.ToStringEmptyText; - otherwise, return the result of
ToString.
Keep in mind that instances of nullable value types will be passed boxed and therefore seen as either null or a non-nullable value.
Constants
| Name | Description |
|---|---|
| NullText | The text used by ExceptionHelper.FormatObject to represent null values. |
| ToStringEmptyText | The text used by ExceptionHelper.FormatObject to represent values whose ToString method returns the empty string. |
| ToStringNullText | The text used by ExceptionHelper.FormatObject to represent values whose ToString method returns null. |
NullText
The text used by ExceptionHelper.FormatObject to represent null values.
public const string NullText = "<null>"ToStringEmptyText
The text used by ExceptionHelper.FormatObject to represent values whose ToString method returns the empty string.
public const string ToStringEmptyText = "<empty!>"ToStringNullText
The text used by ExceptionHelper.FormatObject to represent values whose ToString method returns null.
public const string ToStringNullText = "<null!>"