我在userinfo表中有一些用户信息,如果代码执行失败意味着控制将会捕获块,我可以在catchy块中返回什么。在catch块中返回什么
public static UsersInfo GetById(int Userid)
{
if (Userid <= null)
{
throw new ArgumentNullException();
}
try
{
UnitOfWork unitofwork = new UnitOfWork();
var user = unitofwork.UserInfoRepository.GetByID(Userid);
return user;
}
catch(Exception ex)
{
// What to return in catch block ?
}
}
那么,在这种情况下,它已经到了要返回的东西了。您可以根据您的需要返回'null'或者'UsersInfo'的某种填充实例作为“默认”值。 –
你想从'catch'返回什么? – Dennis
这取决于你想要的。您可以向呼叫者抛出异常,以便呼叫者知道呼叫有什么问题。在投掷之前,您可能需要记录异常以便以后进行故障排除和分析。如果调用者不需要知道错误,那么你可以返回null。 – Adil