Win32Exception 類別

定義

會拋出 Win32 錯誤代碼的例外。

public ref class Win32Exception : Exception
public ref class Win32Exception : System::Runtime::InteropServices::ExternalException
public class Win32Exception : Exception
public class Win32Exception : System.Runtime.InteropServices.ExternalException
[System.Serializable]
public class Win32Exception : System.Runtime.InteropServices.ExternalException
type Win32Exception = class
    inherit Exception
type Win32Exception = class
    inherit ExternalException
type Win32Exception = class
    inherit ExternalException
    interface ISerializable
[<System.Serializable>]
type Win32Exception = class
    inherit ExternalException
    interface ISerializable
Public Class Win32Exception
Inherits Exception
Public Class Win32Exception
Inherits ExternalException
繼承
Win32Exception
繼承
衍生
屬性
實作

範例

以下程式碼範例說明如何捕捉 Win32 例外並解讀其內容。 範例嘗試啟動一個不存在的執行檔,導致 Win32 例外。 當偵測到例外時,範例會擷取該例外的錯誤訊息、程式碼及來源。

try
{
   System::Diagnostics::Process^ myProc = gcnew System::Diagnostics::Process;
   //Attempting to start a non-existing executable
   myProc->StartInfo->FileName = "c:\nonexist.exe";
   //Start the application and assign it to the process component.
   myProc->Start();
}
catch ( Win32Exception^ w ) 
{
   Console::WriteLine( w->Message );
   Console::WriteLine( w->ErrorCode );
   Console::WriteLine( w->NativeErrorCode );
   Console::WriteLine( w->StackTrace );
   Console::WriteLine( w->Source );
   Exception^ e = w->GetBaseException();
   Console::WriteLine( e->Message );
}
try
{
    System.Diagnostics.Process myProc = new();
    myProc.StartInfo.FileName = @"c:\nonexist.exe"; // Attempt to start a non-existent executable
    _ = myProc.Start();
}
catch (Win32Exception w)
{
    Console.WriteLine(w.Message);
    Console.WriteLine(w.ErrorCode.ToString());
    Console.WriteLine(w.NativeErrorCode.ToString());
    Console.WriteLine(w.StackTrace);
    Console.WriteLine(w.Source);
    Exception e = w.GetBaseException();
    Console.WriteLine(e.Message);
}
Try
    Dim myProc As New System.Diagnostics.Process()
    myProc.StartInfo.FileName = "c:\nonexist.exe"  'Attempting to start a non-existing executable
    myProc.Start()    'Start the application and assign it to the process component.    

Catch w As System.ComponentModel.Win32Exception
    Console.WriteLine(w.Message)
    Console.WriteLine(w.ErrorCode.ToString())
    Console.WriteLine(w.NativeErrorCode.ToString())
    Console.WriteLine(w.StackTrace)
    Console.WriteLine(w.Source)
    Dim e As New Exception()
    e = w.GetBaseException()
    Console.WriteLine(e.Message)
End Try

備註

Win32 錯誤碼在顯示時會從數字表示轉換成系統訊息。 用 NativeErrorCode 來存取與此例外相關的錯誤代碼的數字表示。 欲了解更多錯誤代碼資訊,請參見 Win32 錯誤代碼

建構函式

名稱 Description
Win32Exception()

初始化一個新的類別實例 Win32Exception ,並帶有最後一次 Win32 錯誤。

Win32Exception(Int32, String)

初始化一個新實例, Win32Exception 並以指定的錯誤與詳細描述。

Win32Exception(Int32)

初始化一個新的類別實例 Win32Exception ,並以指定的錯誤值。

Win32Exception(SerializationInfo, StreamingContext)
已淘汰.

初始化一個新的類別實例 Win32Exception ,並依指定的上下文與序列化資訊。

Win32Exception(String, Exception)

初始化一個帶有指定詳細描述與例外的新類別實例 Win32Exception

Win32Exception(String)

初始化一個包含特定詳細描述的 Win32Exception 類別新實例。

屬性

名稱 Description
Data

取得一組鍵值對,提供關於例外的額外使用者定義資訊。

(繼承來源 Exception)
ErrorCode

明白 HRESULT 了錯誤。

(繼承來源 ExternalException)
HelpLink

取得或設定與此例外相關的說明檔案連結。

(繼承來源 Exception)
HResult

取得或設定 HRESULT,一個編碼的數值,指派給特定例外。

(繼承來源 Exception)
InnerException

會取得 Exception 造成目前例外的實例。

(繼承來源 Exception)
Message

取得描述目前例外狀況的訊息。

(繼承來源 Exception)
NativeErrorCode

會收到與此例外相關的 Win32 錯誤代碼。

Source

取得或設定造成錯誤之應用程式或物件的名稱。

(繼承來源 Exception)
StackTrace

會取得呼叫堆疊上即時框架的字串表示。

(繼承來源 Exception)
TargetSite

會取得拋出當前例外的方法。

(繼承來源 Exception)

方法

名稱 Description
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetBaseException()

當在派生類別中被覆寫時,回傳 Exception 是一個或多個後續例外的根因。

(繼承來源 Exception)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

會以檔案名稱和發生此事SerializationInfo的行號來設定物件。Win32Exception

GetType()

取得目前實例的執行時型態。

(繼承來源 Exception)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

回傳包含 NativeErrorCode、 或 HResult,或兩者皆有的字串。

ToString()

建立並回傳當前例外的字串表示。

(繼承來源 Exception)
ToString()

回傳包含錯誤 HRESULT 的字串。

(繼承來源 ExternalException)

事件

名稱 Description
SerializeObjectState
已淘汰.

當例外被序列化以建立包含該例外序列化資料的例外狀態物件時,會發生這種情況。

(繼承來源 Exception)

適用於