PetaPoco不会抛出异常

时间:2016-08-23 15:43:53

标签: c# petapoco

我正在使用PetaPoco并使用以下代码从数据库中获取名称列表。

public IEnumerable<string> GetManufacturers()
{
    try
    {
        statement = "EXEC P_GET_ALL_MANUFACTURER_NAMES";
        var data = db.Query<string>(statement);
        return data;
    }
    catch (SqlException sqlex)
    {
        LogManager.WriteException(sqlex);
        throw;
    }
    catch (InvalidOperationException ioex)
    {
        LogManager.WriteException(ioex);
        throw;
    }
}

当数据库中不存在存储过程时,我注意到异常没有被catch块捕获并且被写入正在写入上面API层的data变量它被渲染为JSON对象的位置。

{
    "Message" : "An error has occurred.",
    "ExceptionMessage" : "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.",
    "ExceptionType" : "System.InvalidOperationException",
    "StackTrace" : null,
    "InnerException" : {
        "Message" : "An error has occurred.",
        "ExceptionMessage" : "Could not find stored procedure 'P_GET_ALL_MANUFACTURER_NAMES'.",
        "ExceptionType" : "System.Data.SqlClient.SqlException",
        "StackTrace" : "..."
    }
}

我在这里做错了什么?如何让PetaPoco抛出异常?

1 个答案:

答案 0 :(得分:0)

你不是在重新投掷吗?

throw;