[英]On Error GoTo ErrHand(C#)
How in C# if I have error can I send it to an error handling line like below. 如果发生错误,如何在C#中将其发送到如下的错误处理行。 I know how to do it in visual basic, but need a little assistance in C#.
我知道如何在Visual Basic中做到这一点,但在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: .NET中的“出错时跳转”模式已升级为:
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? 您缺少许多C#基本信息,是时候进行一些培训了吗?
try
{
//Code here
}catch(Exception ex)
{
HandleExeption(ex)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.