Exception.ToString 方法

定義

建立並傳回目前例外狀況的字串表示。

public:
 override System::String ^ ToString();
public override string ToString ();
override this.ToString : unit -> string
Public Overrides Function ToString () As String

傳回

目前例外狀況的字串表示。

實作

範例

下列範例會造成例外狀況,並顯示在該例外狀況上呼叫 ToString 的結果。 請注意, Exception.ToString 當 Exception 類別實例出現在方法的 Console.WriteLine 引數清單中時,會隱含呼叫 方法。

using namespace System;

public ref class TestClass{};

int main()
{
   TestClass^ test = gcnew TestClass;
   array<Object^>^ objectsToCompare = { test, test->ToString(), 123,
                                        (123).ToString(), "some text",
                                        "Some Text" };
   String^ s = "some text";
   for each (Object^ objectToCompare in objectsToCompare) {
      try {
         Int32 i = s->CompareTo(objectToCompare);
         Console::WriteLine("Comparing '{0}' with '{1}': {2}",
                            s, objectToCompare, i);
      }
      catch (ArgumentException^ e) {
            Console::WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare->GetType()->Name);
            Console::WriteLine("Exception information: {0}", e);
      }
      Console::WriteLine();
   }
}
// The example displays the following output:
//     Bad argument: TestClass (type TestClass)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at Example.Main()
//     
//     Comparing 'some text' with 'TestClass': -1
//     
//     Bad argument: 123 (type Int32)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at Example.Main()
//     
//     Comparing 'some text' with '123': 1
//     
//     Comparing 'some text' with 'some text': 0
//     
//     Comparing 'some text' with 'Some Text': -1
using System;

public class TestClass
{}

public class Example
{
   public static void Main()
   {
      var test = new TestClass();
      Object[] objectsToCompare = { test, test.ToString(), 123,
                                    123.ToString(), "some text",
                                    "Some Text" };
      string s = "some text";
      foreach (var objectToCompare in objectsToCompare) {
         try {
            int i = s.CompareTo(objectToCompare);
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i);
         }
         catch (ArgumentException e) {
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name);
            Console.WriteLine("Exception information: {0}", e);
         }
         Console.WriteLine();
      }
   }
}
// The example displays the following output:
//     Bad argument: TestClass (type TestClass)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at Example.Main()
//
//     Comparing 'some text' with 'TestClass': -1
//
//     Bad argument: 123 (type Int32)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at Example.Main()
//
//     Comparing 'some text' with '123': 1
//
//     Comparing 'some text' with 'some text': 0
//
//     Comparing 'some text' with 'Some Text': -1
open System

type TestClass() = class end

let test = TestClass()
let objectsToCompare: obj[] =
    [| test; test.ToString(); 123
       string 123; "some text"
       "Some Text" |]

let s = "some text"
for objectToCompare in objectsToCompare do
    try
        let i = s.CompareTo objectToCompare
        printfn $"Comparing '{s}' with '{objectToCompare}': {i}"
    with :? ArgumentException as e ->
        printfn $"Bad argument: {objectToCompare} (type {objectToCompare.GetType().Name})"
        printfn $"Exception information: {e}"
    printfn ""

// The example displays the following output:
//     Bad argument: Example+TestClass (type TestClass)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at <StartupCode$fs>.$Example.main@()
//
//     Comparing 'some text' with 'Example+TestClass': -1
//
//     Bad argument: 123 (type Int32)
//     Exception information: System.ArgumentException: Object must be of type String.
//        at System.String.CompareTo(Object value)
//        at <StartupCode$fs>.$Example.main@()
//
//     Comparing 'some text' with '123': 1
//
//     Comparing 'some text' with 'some text': 0
//
//     Comparing 'some text' with 'Some Text': -1
Public Class TestClass
End Class 

Public Class Example
   Public Shared Sub Main()
      Dim test As New TestClass()
      Dim objectsToCompare() As Object = { test, test.ToString(), 123,
                                           123.ToString(), "some text",
                                           "Some Text" }
      Dim s As String = "some text"
      For Each objectToCompare In objectsToCompare
         Try
            Dim i As Integer = s.CompareTo(objectToCompare)
            Console.WriteLine("Comparing '{0}' with '{1}': {2}",
                              s, objectToCompare, i)
         Catch e As ArgumentException
            Console.WriteLine("Bad argument: {0} (type {1})",
                              objectToCompare,
                              objectToCompare.GetType().Name)
            Console.WriteLine("Exception information: {0}", e)
         End Try
         Console.WriteLine()
      Next
   End Sub 
End Class 
' The example displays the following output:
'     Bad argument: TestClass (type TestClass)
'     Exception information: System.ArgumentException: Object must be of type String.
'        at System.String.CompareTo(Object value)
'        at Example.Main()
'     
'     Comparing 'some text' with 'TestClass': -1
'     
'     Bad argument: 123 (type Int32)
'     Exception information: System.ArgumentException: Object must be of type String.
'        at System.String.CompareTo(Object value)
'        at Example.Main()
'     
'     Comparing 'some text' with '123': 1
'     
'     Comparing 'some text' with 'some text': 0
'     
'     Comparing 'some text' with 'Some Text': -1

備註

ToString 會傳回目前例外狀況的標記法,此例外狀況旨在供人類瞭解。 如果例外狀況包含區分文化特性的資料,則需要傳 ToString 回的字串標記法,才能考慮目前的系統文化特性。 雖然傳回字串的格式沒有確切的需求,但應該嘗試反映使用者所察覺的物件值。

的預設實 ToString 作會取得擲回目前例外狀況的類別名稱、訊息、在內部例外狀況上呼叫 ToString 的結果,以及呼叫 Environment.StackTrace 的結果。 如果其中任一成員為 null ,則傳回的字串中不包含其值。

如果沒有錯誤訊息,或它是空字串 (「」) ,則不會傳回任何錯誤訊息。 只有在不是 時 null ,才會傳回內部例外狀況和堆疊追蹤的名稱。

這個方法會覆寫 Object.ToString

適用於