Exception.ToString 메서드

정의

현재 예외에 대한 문자열 표현을 만들고 반환합니다.

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

반환

String

현재 예외에 대한 문자열 표현입니다.

구현

예제

다음 예제에서는 예외를 발생시키고 해당 예외에 대한 호출 ToString 결과를 표시합니다. 메서드의 Exception.ToString 인수 목록에 Console.WriteLine Exception 클래스 인스턴스가 표시되면 메서드가 암시적으로 호출됩니다.

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 은 현재 예외를 throw한 클래스의 이름, 메시지, 내부 예외 호출 결과 및 호출 ToString Environment.StackTrace결과를 가져옵니다. 이러한 멤버 null가 있으면 해당 값이 반환된 문자열에 포함되지 않습니다.

오류 메시지가 없거나 빈 문자열("")인 경우 오류 메시지가 반환되지 않습니다. 내부 예외의 이름과 스택 추적은 그렇지 않은 null경우에만 반환됩니다.

이 메서드는 Object.ToString를 재정의합니다.

적용 대상