PetaPoco不会抛出异常

本文关键字:抛出异常 PetaPoco | 更新日期: 2023-09-27 18:03:33

我正在使用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抛出异常?

PetaPoco不会抛出异常

你不重新扔吗?

throw;