RaiseError 没有引发足够的错误?

发布于 2024-09-17 07:35:18 字数 516 浏览 5 评论 0原文

为什么UI层没有出现错误?我正在使用 ExecuteScaler

BEGIN CATCH

  PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
  ROLLBACK TRANSACTION;

  DECLARE @ErrorMessage NVARCHAR(4000);
  DECLARE @ErrorSeverity INT;
  DECLARE @ErrorState INT;

  SELECT @ErrorMessage = ERROR_MESSAGE(),
         @ErrorSeverity = ERROR_SEVERITY(),
         @ErrorState = ERROR_STATE();

  RAISERROR (@ErrorMessage, 
             @ErrorSeverity,
             @ErrorState);

END CATCH

谢谢

Why the error does not appear at the UI layer? I am using ExecuteScaler

BEGIN CATCH

  PRINT N'The transaction is in an uncommittable state. Rolling back transaction.'
  ROLLBACK TRANSACTION;

  DECLARE @ErrorMessage NVARCHAR(4000);
  DECLARE @ErrorSeverity INT;
  DECLARE @ErrorState INT;

  SELECT @ErrorMessage = ERROR_MESSAGE(),
         @ErrorSeverity = ERROR_SEVERITY(),
         @ErrorState = ERROR_STATE();

  RAISERROR (@ErrorMessage, 
             @ErrorSeverity,
             @ErrorState);

END CATCH

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风和你 2024-09-24 07:35:18

只能引发用户消息,系统消息只能由引擎引发:

用户定义错误的错误号
消息数应大于 50000。
当未指定msg_id时,
RAISERROR 会引发错误消息
错误号为 50000。

因此您无法引发原始 @ErrorMessage,而必须引发 错误代码。

此外,您的 catch 块是不正确的,因为它假设一个不可提交的事务。情况并非总是如此,您必须检查XACT_STATE() 在决定交易是否注定失败之前。在许多情况下,错误处理可以继续事务。请参阅异常处理和嵌套事务

You can only raise user messages, system messages can only be raised by the engine:

Error numbers for user-defined error
messages should be greater than 50000.
When msg_id is not specified,
RAISERROR raises an error message with
an error number of 50000.

Therefore you cannot raise the original @ErrorMessage, you have to raise a new error code.

Also, your catch block is incorrect in as it assumes an uncommittable transaction. This is not always the case, you must check the result of XACT_STATE() before deciding if the transaction is doomed. There are many cases on which the error handling can continue the transaction. See Exception handling and nested transactions.

深陷 2024-09-24 07:35:18

首先,查看 Remus 关于嵌套事务中的错误处理的文章,以便您了解全部内容。

然后,尝试将错误级别强制为 16。此外,在重新抛出错误时将原始错误信息嵌入到错误消息中,这样就不会丢失该信息:

BEGIN TRY
  SELECT 1/0
END TRY

BEGIN CATCH

  DECLARE 
    @ErrorMessage  NVARCHAR(4000)
  , @ErrorNumber   INT
  , @ErrorSeverity INT
  , @ErrorState    INT
  , @ErrorLine     INT

  SELECT 
    @ErrorNumber   = ERROR_NUMBER()
  , @ErrorSeverity = ERROR_SEVERITY()
  , @ErrorState    = ERROR_STATE()
  , @ErrorLine     = ERROR_LINE();

  SET @ErrorMessage 
    = '>> Msg %i, Level %i, State %i, Line %i'
    + CHAR(13)+CHAR(10)+ERROR_MESSAGE()

  RAISERROR (
    @ErrorMessage,16,1
  , @ErrorNumber
  , @ErrorSeverity
  , @ErrorState
  , @ErrorLine
  )

END CATCH

First, review Remus's article on error handling in nested transactions, so you understand the full scope of things.

Then, try forcing the error level to 16. Also, embed the original error information in the error message when you rethrow the error, so you don't lose that information:

BEGIN TRY
  SELECT 1/0
END TRY

BEGIN CATCH

  DECLARE 
    @ErrorMessage  NVARCHAR(4000)
  , @ErrorNumber   INT
  , @ErrorSeverity INT
  , @ErrorState    INT
  , @ErrorLine     INT

  SELECT 
    @ErrorNumber   = ERROR_NUMBER()
  , @ErrorSeverity = ERROR_SEVERITY()
  , @ErrorState    = ERROR_STATE()
  , @ErrorLine     = ERROR_LINE();

  SET @ErrorMessage 
    = '>> Msg %i, Level %i, State %i, Line %i'
    + CHAR(13)+CHAR(10)+ERROR_MESSAGE()

  RAISERROR (
    @ErrorMessage,16,1
  , @ErrorNumber
  , @ErrorSeverity
  , @ErrorState
  , @ErrorLine
  )

END CATCH
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文