C# Class Loyc.ExceptionExt

Extension methods for exceptions.
显示文件 Open project: qwertie/ecsharp

Public Methods

Method Description
AppendDataList ( IDictionary dict, StringBuilder sb, string linePrefix, string keyValueSeparator, string newLine ) : StringBuilder
DataList ( this ex ) : string

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

DataList ( this ex, string linePrefix, string keyValueSeparator, string newLine ) : string

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.

Description ( this ex ) : string
Description ( this ex, bool addStackTrace, string lineSeparator = "\n\n" ) : string

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).

DescriptionAndStackTrace ( this ex ) : string Adds a stack trace.
ExceptionMessageAndType ( this ex ) : string

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

InnermostException ( this ex ) : Exception

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

PreserveStackTrace ( this exception ) : void

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.

ToDetailedString ( this ex ) : string

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.

ToDetailedString ( this ex, int maxInnerExceptions ) : string

Method Details

AppendDataList() public static method

public static AppendDataList ( IDictionary dict, StringBuilder sb, string linePrefix, string keyValueSeparator, string newLine ) : StringBuilder
dict IDictionary
sb StringBuilder
linePrefix string
keyValueSeparator string
newLine string
return StringBuilder

DataList() public static method

Converts Exception.Data to a string, separating each key-value pair by a newline.
public static DataList ( this ex ) : string
ex this
return string

DataList() public static method

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.
public static DataList ( this ex, string linePrefix, string keyValueSeparator, string newLine ) : string
ex this
linePrefix string
keyValueSeparator string
newLine string
return string

Description() public static method

public static Description ( this ex ) : string
ex this
return string

Description() public static method

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).
public static Description ( this ex, bool addStackTrace, string lineSeparator = "\n\n" ) : string
ex this
addStackTrace bool If 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).
lineSeparator string Separator between different exceptions and /// before the stack trace.
return string

DescriptionAndStackTrace() public static method

Adds a stack trace.
public static DescriptionAndStackTrace ( this ex ) : string
ex this
return string

ExceptionMessageAndType() public static method

Returns a string of the form "{ex.Message} ({ex.GetType().Name})".
public static ExceptionMessageAndType ( this ex ) : string
ex this
return string

InnermostException() public static method

Gets the innermost InnerException, or ex itself if there are no inner exceptions.
ex is null.
public static InnermostException ( this ex ) : Exception
ex this
return System.Exception

PreserveStackTrace() public static method

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.
public static PreserveStackTrace ( this exception ) : void
exception this
return void

ToDetailedString() public static method

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.
public static ToDetailedString ( this ex ) : string
ex this
return string

ToDetailedString() public static method

public static ToDetailedString ( this ex, int maxInnerExceptions ) : string
ex this
maxInnerExceptions int
return string