繁体   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