简体   繁体   中英

On Error GoTo ErrHand(C#)

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

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.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM