什么是Win32Exception
就是封裝了Win32 Error Code的異常。也就是GetLastError返回的值。Win32錯誤代碼在顯示時從其數字表示形式轉換為系統消息。使用NativeErrorCode訪問與此異常關聯的錯誤代碼的數字表示形式
繼承關系
-
ObjectExceptionSystemExceptionExternalExceptionWin32Exception
有如下子類
-
System.Net.HttpListenerExceptionSystem.Net.NetworkInformation.NetworkInformationExceptionSystem.Net.Sockets.SocketExceptionSystem.Net.WebSockets.WebSocketException
HRESULT
80004005
下面的代碼示例演示如何捕獲Win32異常並解釋其內容。該示例嘗試啟動不存在的可執行文件,這將導致Win32異常。在捕捉到異常時,該示例獲取相應的錯誤消息、代碼和異常的來源。
try { System.Diagnostics.Process myProc = 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(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); }