错误时转到错误手(C#)

错误时转到错误手(C#),c#,error-handling,C#,Error Handling,在C#中,如果我有错误,如何将其发送到下面这样的错误处理行。我知道如何用VisualBasic来做,但需要C#的一些帮助。谢谢你的帮助 Sub Main() On Error GoTo ErrHand ....Code Here End Sub ErrHand: MsgBox "Message Here" End Sub On Error GoTo模式在.NET中升级为: try { // Execute your code } catch <ExceptionType&g

在C#中,如果我有错误,如何将其发送到下面这样的错误处理行。我知道如何用VisualBasic来做,但需要C#的一些帮助。谢谢你的帮助

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

ErrHand:
  MsgBox "Message Here"
End Sub

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

try
{
   // Execute your code
}
catch  <ExceptionType>
{
 // Handle exception
}
finally
{
 // Cleanup resources
}
试试看
{
//执行你的代码
}
抓住
{
//处理异常
}
最后
{
//清理资源
}

下面的链接应该为您提供一些信息。

错误转到模式在.NET中升级为:

try
{
   // Execute your code
}
catch  <ExceptionType>
{
 // Handle exception
}
finally
{
 // Cleanup resources
}
try
{
   //your code here
}
catch
{
   // error handling here
}
试试看
{
//执行你的代码
}
抓住
{
//处理异常
}
最后
{
//清理资源
}
下面的链接应该会给你一些信息。

你错过了很多基本信息,是时候进行一些培训了

try
{
   //your code here
}
catch
{
   // error handling here
}
try
{
    //Code here
}catch(Exception ex)
{
    HandleExeption(ex)
}
你错过了很多C#基本信息,是时候进行一些培训了

try
{
    //Code here
}catch(Exception ex)
{
    HandleExeption(ex)
}
我可以(逻辑上)完成,但它不是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;
    }
我可以(逻辑上)完成,但它不是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;
    }

有几件事需要注意——finally不是必需的,但是如果您有任何资源可以释放(通常这是一种流),那么拥有它是很好的。你也可以设置多个catch语句来处理不同类型的异常。谢谢你的帮助,我很高兴你可以使用多个catch语句,因为有很多不同的senarios。真的很感激。有几件事需要注意——最后一件不是必需的,但是如果你有任何资源可以释放的话(通常这是一种流)。你也可以设置多个catch语句来处理不同类型的异常。谢谢你的帮助,我很高兴你可以使用多个catch语句,因为有很多不同的senarios。非常感谢。