SingleOrDefault异常处理 [英] SingleOrDefault exception handling

查看:53
本文介绍了SingleOrDefault异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个萨姆代码,它会调用SingleOrDefault方法3次,并在任何序列具有多个匹配元素的情况下记录异常.

I have a samle code which calls SingleOrDefault method 3 times and logs exception if any sequence has more than one matching element.

如果我想检查这段代码的哪一部分引发异常,问题就开始了.

The problem starts if I want to check which part of this code throws exception.

是否可以从此异常中获取一些有用的信息,例如谓词参数或收集类型以获取更详细的跟踪信息?

Is it possible to get some useful information from this exception like predicate parameter or collection type for more detailed trace?

-序列包含多个匹配元素.集合IEnumrable | ParamType |param {谓词para to toString()}

 public void GetSingleOrDefaultTest(){

    try{

        var user = Users.SingleOrDefault(e => e.Id == 1);

        var profile = UserProfiles.SingleOrDefault(e => e.Id == 1);

        var profile2 = UserProfiles.SingleOrDefault(e => e.Id == 2);


    } catch(InvalidOperationException ex){
        Log(ex);
    }

}

推荐答案

如果您想知道哪个语句发出了错误,则必须单独检查它们.在每个 SingleOrDefault 调用上捕获 InvalidOperationException ,并将其包装在新的异常中,您可以在其中填充其他信息.

If you want to know which of the statement issues the error you have to check them separateley. Catch the InvalidOperationException on every SingleOrDefault invocation and wrap it in a new exception which you can fill with additional information.

try
{
    User user;
    UserProfile profile;
    UserProfile profile2;

    try
    {
        user = Users.SingleOrDefault(e => e.Id == 1);
    }
    catch (InvalidOperationException ex)
    {
        throw new InvalidOperationException("User lookup for Id = 1 failed", ex);
    }

    try
    {
        profile = UserProfiles.SingleOrDefault(e => e.Id == 1);
    }
    catch (InvalidOperationException ex)
    {
        throw new InvalidOperationException("User profile lookup for Id = 1 failed", ex);
    }

    try
    {
        profile2 = UserProfiles.SingleOrDefault(e => e.Id == 2);
    }
    catch (InvalidOperationException ex)
    {
        throw new InvalidOperationException("User profile lookup for Id = 2 failed", ex);
    }

    // work with user, profile and profile2
}
catch(InvalidOperationException ex)
{
    Log(ex);
}

您还可以通过以下方法封装单个try捕获

You also can encapsulate the single try catches by the following

private static T GetSingleOrDefault<T>(IEnumerable<T> collection, Expression<Func<T, bool>> predicate)
{
    try
    {
        return collection.SingleOrDefault(predicate.Compile());
    }
    catch (InvalidOperationException e)
    {
        var message = string.Format(
            "{0} (Collection: {1}, param: {2})",
            e.Message,
            collection.GetType(),
            predicate);

        throw new InvalidOperationException(message);
    }
}

使您的代码看起来像

try
{
    var user = GetSingleOrDefault(Users, e => e.Id == 1);

    var profile = GetSingleOrDefault(UserProfiles, e => e.Id == 1);

    var profile2 = GetSingleOrDefault(UserProfiles, e => e.Id == 2);

    // work with user, profile and profile2
}
catch(InvalidOperationException ex)
{
    Log(ex);
}

这会产生类似消息

System.InvalidOperationException:序列包含多个匹配元素(集合:IEnumerable`1 [User],参数:e => e.Id == 1)

System.InvalidOperationException: Sequence contains more than one matching element (Collection: IEnumerable`1[User], param: e => e.Id == 1)

这篇关于SingleOrDefault异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆