简体   繁体   English

try / catch块未捕获异常

[英]try/catch block not catching an exception

I am having an issue where an exception is not being caught by try/catch block. 我遇到一个问题,即try / catch块未捕获异常。 The issue occurs at command.ExecuteReader() , however it never gets caught. 该问题发生在command.ExecuteReader()处 ,但从未被发现。 I am running in debug mode and have already tried a few suggested options in regards to the debugger settings with no avail. 我正在调试模式下运行,并且已经尝试了一些有关调试器设置的建议选项,但无济于事。

I do want to mention that I am using SQLite as my provider, and I can see that it throws an SQLiteException, however the issue remains. 我确实要提到我正在使用SQLite作为提供程序,并且可以看到它引发了SQLiteException,但是问题仍然存在。 Would there be any specific scenario where an exception is not caught? 在特定情况下不会捕获异常吗? (with exception of StackOverflowException, ThreadAbortedException etc...) (除了StackOverflowException,ThreadAbortedException等...)

    public IEnumerable<dynamic> Query(string sql, params object[] parms)
    {
        try
        {
            return QueryCore(sql, parms);
        }
        catch (Exception ex)
        {
            throw new DbException(sql, parms, ex);
        }
    }

    private IEnumerable<dynamic> QueryCore(string sql, params object[] parms)
    {
        using (var connection = CreateConnection())
        {
            using (var command = CreateCommand(sql, connection, parms))
            {
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        yield return reader.ToExpando();
                    }
                }
            }
        }
    }

I also want to add that if I produce a correct query against the database, I get results back, however when I break the query, the exception is thrown, however not caught. 我还想补充一点,如果我对数据库产生正确的查询,我会得到结果,但是当我中断查询时,会引发异常,但是不会被捕获。

This happens because you are returning the data with the yield keyword. 发生这种情况是因为您要使用yield关键字返回数据。 This makes the data actual data method to run only when it's results are enumerated. 这使得数据实际数据方法仅在枚举结果时才运行。

You probably don't want this to happen, especially because if the results are enumerated twice (eg two seperate foreach loops) the data will be read twice. 您可能不希望发生这种情况,尤其是因为如果将结果枚举两次(例如,两个单独的foreach循环),则数据将被读取两次。 You can do this to make the enumeration to happen immediately and catch any exception: 您可以执行以下操作使枚举立即发生并捕获任何异常:

public IEnumerable<dynamic> Query(string sql, params object[] parms)
{
    try
    {
        return QueryCore(sql, parms).ToArray();
    }
    catch (Exception ex)
    {
        throw new DbException(sql, parms, ex);
    }
}

Yielding is good for situations where getting an item takes some time, and you don't want to get ALL the items before you can loop through them. 对于需要花费一些时间才能获得物品的情况,并且在循环浏览它们之前,您并不想获得所有物品,Yielding很有用。 So another possible solution, that might be better for the readability of your code (that I assume doesn't need to yield) will be this: 因此,另一种可能的解决方案可能是这样:对于代码的可读性更好(我认为不需要屈服):

public IEnumerable<dynamic> Query(string sql, params object[] parms)
{
    try
    {
        return QueryCore(sql, parms);
    }
    catch (Exception ex)
    {
        throw new DbException(sql, parms, ex);
    }
}

private IEnumerable<dynamic> QueryCore(string sql, params object[] parms)
{
    using (var connection = CreateConnection())
    {
        using (var command = CreateCommand(sql, connection, parms))
        {
            using (var reader = command.ExecuteReader())
            {
                var results = new List<dynamic>();
                while (reader.Read())
                {
                    results.Add(reader.ToExpando());
                }

                return results;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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