Exception.ToString 方法

创建并返回当前异常的字符串表示形式。

**命名空间:**System
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overrides Function ToString As String
用法
Dim instance As Exception
Dim returnValue As String

returnValue = instance.ToString
public override string ToString ()
public:
virtual String^ ToString () override
public String ToString ()
public override function ToString () : String

返回值

当前异常的字符串表示形式。

备注

ToString 返回当前异常的可读表示形式。当该异常包含区分区域性的数据时,ToString 所返回的字符串表示形式需要考虑当前系统区域性。虽然对于所返回字符串的格式没有确切的要求,但应当努力从用户的角度反映对象的值。

ToString 的默认实现获取引发当前异常的类名、消息、对内部异常调用 ToString 的结果和调用 Environment.StackTrace 的结果。如果这些成员中有任何为 空引用(在 Visual Basic 中为 Nothing),则它的值不包含在返回的字符串中。

如果没有错误信息或者错误信息是空字符串 (""),则不返回错误信息。仅当内部异常的名称和堆栈跟踪不为 空引用(在 Visual Basic 中为 Nothing) 时,才返回它们。

此方法重写 Object.ToString

示例

下面的示例将导致异常并显示对该异常调用 ToString 的结果。

Imports System

Public Class [MyClass]
End Class '[MyClass]

Public Class ArgExceptionExample
   
   Public Shared Sub Main()
      Dim my As New [MyClass]()
      Dim s As String = "sometext"
      Try
         Dim i As Integer = s.CompareTo(my)
      Catch e As Exception
         Console.WriteLine("Error: {0}", e.ToString())
      End Try
   End Sub 'Main
End Class 'ArgExceptionExample
using System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static void Main()
      {
      MyClass my = new MyClass();
      string s = "sometext";
      try 
         {
         int i = s.CompareTo(my);
         }
            catch (Exception e) 
            {
            Console.WriteLine("Error: {0}",e.ToString());
            }
      }
}
using namespace System;
public ref class MyClass{};

int main()
{
   MyClass^ my = gcnew MyClass;
   String^ s = "sometext";
   try
   {
      int i = s->CompareTo( my );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Error: {0}", e->ToString() );
   }

}
import System.*;

public class MyClass
{
} //MyClass

public class ArgExceptionExample
{
    public static void main(String[] args)
    {
        MyClass my = new MyClass();
        String s = "sometext";
        try {
            int i = s.CompareTo(my);
        }
        catch (System.Exception e) {
            Console.WriteLine("Error: {0}", e.ToString());
        }
    } //main
} //ArgExceptionExample
import System;

public class MyClass {}
public class ArgExceptionExample 
   {
   public static function Main()
      {
      var my : MyClass = new MyClass();
      var s : String = "sometext";
      try 
         {
         var i : int = s.CompareTo(my);
         }
     catch (e : Exception) 
         {
         Console.WriteLine("Error: {0}",e.ToString());
         }
      }
}

此代码的输出如下:

Error: System.ArgumentException: Object must be of type String. at System.String.CompareTo(Object value) at ArgExceptionExample.Main()

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Exception 类
Exception 成员
System 命名空间