System.Exception.ToString C# (CSharp) Method

ToString() public method

public ToString ( ) : String
return String
        public override String ToString() {
            String message = Message;
            String s;
            if (_className==null) {
                _className = GetClassName();
            }

            if (message == null || message.Length <= 0) {
                s = _className;
            }
            else {
                s = _className + ": " + message;
            }

            if (_innerException!=null) {
                s = s + " ---> " + _innerException.ToString() + Environment.NewLine + "   " + Environment.GetResourceString("Exception_EndOfInnerExceptionStack");
            }


            if (StackTrace != null)
                s += Environment.NewLine + StackTrace;

            return s;
        }
    

Usage Example

		/// <summary>
		/// This builds a string detailing the given exception.
		/// </summary>
		/// <param name="ex">The exceptions to describe.</param>
		/// <returns>A string detailing the given exception.</returns>
		public static string CreateTraceExceptionString(Exception ex)
		{
			if (ex == null)
				return "\tNO EXCEPTION.";

			StringBuilder stbException = new StringBuilder();
			stbException.AppendLine("Exception: ");
			stbException.AppendLine("Message: ").Append("\t");
			stbException.AppendLine(ex.Message);
			stbException.AppendLine("Full Trace: ").Append("\t");
			stbException.AppendLine(ex.ToString());
			if (ex is BadImageFormatException)
			{
				BadImageFormatException biex = (BadImageFormatException)ex;
				stbException.AppendFormat("File Name:\t{0}", biex.FileName).AppendLine();
				stbException.AppendFormat("Fusion Log:\t{0}", biex.FusionLog).AppendLine();
			}
			while (ex.InnerException != null)
			{
				ex = ex.InnerException;
				stbException.AppendLine("Inner Exception:");
				stbException.AppendLine(ex.ToString());
			}
			return stbException.ToString();
		}
All Usage Examples Of System.Exception::ToString