出错时转到 ErrHand(C#)

发布于 2024-12-11 14:22:28 字数 209 浏览 0 评论 0原文

在 C# 中,如果出现错误,如何将其发送到错误处理行,如下所示。我知道如何在 Visual Basic 中执行此操作,但在 C# 中需要一些帮助。感谢您的帮助

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

ErrHand:
  MsgBox "Message Here"
End Sub

How in C# if I have error can I send it to an error handling line like below. I know how to do it in visual basic, but need a little assistance in C#. Thanks for the help

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

ErrHand:
  MsgBox "Message Here"
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难理解 2024-12-18 14:22:28

On Error GoTo 模式在 .NET 中升级为:

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

以下链接错误处理转换应该为您提供一些信息。

The On Error GoTo pattern is upgraded in .NET to:

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

The following link Error Handling Transformation should give you some info.

和我恋爱吧 2024-12-18 14:22:28
try
{
   //your code here
}
catch
{
   // error handling here
}
try
{
   //your code here
}
catch
{
   // error handling here
}
柏拉图鍀咏恒 2024-12-18 14:22:28

您缺少大量 C# 基本信息,是时候进行一些培训了吗?

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

You're missing a lot of C# basic information, time for a little training?

try
{
    //Code here
}catch(Exception ex)
{
    HandleExeption(ex)
}
剩一世无双 2024-12-18 14:22:28

我可以完成(逻辑上)但它不是purdy

  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;
    }

I can be done (logically) but it ain't purdy

  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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文