簡體   English   中英

出錯時轉到ErrHand(C#)

[英]On Error GoTo ErrHand(C#)

如果發生錯誤,如何在C#中將其發送到如下的錯誤處理行。 我知道如何在Visual Basic中做到這一點,但在C#中需要一點幫助。 謝謝您的幫助

Sub Main()
On Error GoTo ErrHand
....Code Here
End Sub

ErrHand:
  MsgBox "Message Here"
End Sub

.NET中的“出錯時跳轉”模式已升級為:

try
{
   // Execute your code
}
catch  <ExceptionType>
{
 // Handle exception
}
finally
{
 // Cleanup resources
}

以下鏈接“ 錯誤處理轉換”應該為您提供一些信息。

try
{
   //your code here
}
catch
{
   // error handling here
}

我可以做到(從邏輯上來說),但它並不純粹

  bool TestMethod()
    {
        string _errorMessage = string.Empty;
        bool returnValue = true;
        try
        {
            int x;
            throw new Exception("Force Call To Error Handler");
        }
        catch (Exception ex)
        {
            _errorMessage = ex.ToString();
            goto errHandler;
        }

        //Other code here

        exitCode:
        ;
        return returnValue;

    //Exit code here

    errHandler:
        ;
        //Error Code Here
        returnValue = false;
        goto exitCode;
    }

您缺少許多C#基本信息,是時候進行一些培訓了嗎?

try
{
    //Code here
}catch(Exception ex)
{
    HandleExeption(ex)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM