例如,我们如何在try-catch中返回一些值
try
{
return abc;
}
catch(Exception ex)
{
console.Writeline("Exception occur" + ex);
}发布于 2010-10-18 13:51:26
如果调用该方法,此代码将返回2(发生异常)。
int testException()
{
try
{
int a = 0;
int b = 0;
return a / b;
//return 1;
}
catch (System.Exception ex)
{
return 2;
}
}发布于 2010-10-18 14:13:28
你可以从任何地方返回。
发布于 2010-10-18 13:50:27
Try Catch是一种处理代码中的错误的方法,这些错误导致您需要离开程序的正常流程。许多.net系统组件抛出错误来指示不应将任何代码放入catch块中的条件;
object x;
try
{
x = Func();
}
catch(Exception e)
{
//Set some default Value
x = new Object();
//Send an email error
SendErrorMail();
}
return x;如果您只需要在catch中返回,则可以使用return语句,就像在代码中的其他地方一样。
https://stackoverflow.com/questions/3956819
复制相似问题