Enhanced C#
Language of your choice: library documentation
Static Public Member Functions | List of all members
Loyc.ExceptionExt Class Reference

Extension methods for exceptions. More...


Source file:

Remarks

Extension methods for exceptions.

Static Public Member Functions

static string ExceptionMessageAndType (this Exception ex)
 Returns a string of the form "{ex.Message} ({ex.GetType().Name})". More...
 
static Exception InnermostException (this Exception ex)
 Gets the innermost InnerException, or ex itself if there are no inner exceptions. More...
 
static string Description (this Exception ex)
 
static string DescriptionAndStackTrace (this Exception ex)
 
static string Description (this Exception ex, bool addStackTrace, string lineSeparator="\n\n")
 Gets a description of the exception in the form "{ex.Message} ({ex.GetType().Name})". If the exception has InnerExceptions, these are printed afterward in the form "Inner exception: {ex.Message} ({ex.GetType().Name})" and separated from the outer exception by "\n\n" (or a string of your choosing). More...
 
static string ToDetailedString (this Exception ex)
 Returns a string containing the exception type, message, Data pairs (if any) and stack strace, followed by the type, message and stack strace of inner exceptions, if any. More...
 
static string ToDetailedString (this Exception ex, int maxInnerExceptions)
 
static string DataList (this Exception ex)
 Converts Exception.Data to a string, separating each key-value pair by a newline. More...
 
static string DataList (this Exception ex, string linePrefix, string keyValueSeparator, string newLine)
 Converts Exception.Data to a string, separating each key from each value with keyValueSeparator, prepending each line by linePrefix, and separating each pair with newLine, which may or may not be "\n", your choice. More...
 
static StringBuilder AppendDataList (IDictionary dict, StringBuilder sb, string linePrefix, string keyValueSeparator, string newLine)
 
static void PreserveStackTrace (this Exception exception)
 Calls an internal method of Exception that records an exception's stack trace so that the stack trace does not change if the exception is rethrown (e.g. on another thread). More...
 

Member Function Documentation

static string Loyc.ExceptionExt.DataList ( this Exception  ex)
inlinestatic

Converts Exception.Data to a string, separating each key-value pair by a newline.

static string Loyc.ExceptionExt.DataList ( this Exception  ex,
string  linePrefix,
string  keyValueSeparator,
string  newLine 
)
inlinestatic

Converts Exception.Data to a string, separating each key from each value with keyValueSeparator, prepending each line by linePrefix, and separating each pair with newLine, which may or may not be "\n", your choice.

static string Loyc.ExceptionExt.Description ( this Exception  ex,
bool  addStackTrace,
string  lineSeparator = "\n\n" 
)
inlinestatic

Gets a description of the exception in the form "{ex.Message} ({ex.GetType().Name})". If the exception has InnerExceptions, these are printed afterward in the form "Inner exception: {ex.Message} ({ex.GetType().Name})" and separated from the outer exception by "\n\n" (or a string of your choosing).

Parameters
addStackTraceIf true, the stack trace of the outermost exception is added to the end of the message (not the innermost exception, because the inner stack trace gets truncated. TODO: investigate whether the full stack trace can be reconstructed).
lineSeparatorSeparator between different exceptions and before the stack trace.
static string Loyc.ExceptionExt.DescriptionAndStackTrace ( this Exception  ex)
inlinestatic

Adds a stack trace.

static string Loyc.ExceptionExt.ExceptionMessageAndType ( this Exception  ex)
inlinestatic

Returns a string of the form "{ex.Message} ({ex.GetType().Name})".

static Exception Loyc.ExceptionExt.InnermostException ( this Exception  ex)
inlinestatic

Gets the innermost InnerException, or ex itself if there are no inner exceptions.

Exceptions
NullReferenceExceptionex is null.
static void Loyc.ExceptionExt.PreserveStackTrace ( this Exception  exception)
inlinestatic

Calls an internal method of Exception that records an exception's stack trace so that the stack trace does not change if the exception is rethrown (e.g. on another thread).

Exception ex = null; var thread = new ThreadEx(() => { try { SomethingThatMightThrowOrTakeForever(); } catch (Exception e) { ex = e; ex.PreserveStackTrace(); } }); thread.Start(); if (!thread.Join(timeout)) { thread.Abort(); thread.Join(timeout); } if (ex != null) throw ex; // includes stack trace from the other thread

Note: when rethrowing an exception that was just caught, you should always use "catch;" instead of calling this method.

static string Loyc.ExceptionExt.ToDetailedString ( this Exception  ex)
inlinestatic

Returns a string containing the exception type, message, Data pairs (if any) and stack strace, followed by the type, message and stack strace of inner exceptions, if any.

If maxInnerExceptions is not given, the default is 3.

References Loyc.ExceptionExt.ToDetailedString().

Referenced by Loyc.ExceptionExt.ToDetailedString().