7

在我的存储过程中,我只是检查了一个特定的条件,如果该条件失败,我将抛出异常。如下所示,

Raise Exception '%', 'Hello world';

它工作正常,但是错误消息 Hello world 还带有一些其他错误代码,例如我的前端下面的内容,

DA00014:ERROR: P0001: Hello world
|___________________|
        |
      I really dont want this part to get displayed in the front end.

是否有任何适当的方法可以从抛出的异常中过滤/提取正确的消息。?

[注意: 请不要建议我做一些字符串操作。]

DBMS      : POSTGRESQL 9.0.3
Driver    : Npgsql 1.0
Front End : VB.net
4

4 回答 4

1

Npgsql 快速路径

如果NpgsqlException.Errors在您的异常处理程序中为 null,那么您很可能在 Npgsql Fastpath 中遇到了一个错误(无论如何我认为它是一个错误)。这是版本 2 中的违规行,但版本 1 中存在相同的问题。

它基本上归结为这段代码,其中NpgsqlError构造了一个正确的,然后在通过调用它引发异常时将其丢弃ToString()

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(e.ToString());

修复方法很简单,只需将代码更改为此并使用已支持的功能NpgsqlException来获取构造函数中的列表NpgsqlError

NpgsqlError e = new NpgsqlError(conn.BackendProtocolVersion, stream);
throw new NpgsqlException(new ArrayList(e));

因此,总而言之,除非您编译自己的 Npgsql 版本来修补问题,否则您无法在没有字符串操作的情况下做到这一点。

Npgsql

如果您不使用 Fastpath,则抛出的NpgsqlException对象包含一个Errors应该是非空列表的属性。这是从第一个错误中提取消息的示例。

(ex.Errors[0] as NpgsqlError).Message
于 2013-10-25T03:12:09.583 回答
0

试试这个..
在您的存储过程中,在消息中放置一个额外的字符。例如

Raise Exception '%', '|Hello world';

在前端split your message on character "|",并显示数组的第二个索引。例如

    Try
      ' your code..
    Catch ex As Exception
        Dim arr As String() = ex.Message.Split("|")
        If arr.Count > 1 Then
            MessageBox.Show(Convert.ToString(arr(1)))
        Else
            MessageBox.Show(Convert.ToString(ex.Message))
        End If
    End Try
于 2013-10-25T04:49:40.983 回答
0

如果我明白了,你可以试试这个

IF condition match -- (your condition)
BEGIN
    RAISERROR('Hello world', 16,16)
    return
 END

并且例外

可以使用

catch(exception ex)
{
  // ex.Message
  //  ex.InnerException.Message
}
于 2013-10-30T07:23:16.087 回答
0

这应该有效:

Catch ex As Exception

    lblMsg.Text = ex.Message.Replace("ERROR: P0001: ", "")

End Try
于 2020-02-20T07:06:36.127 回答