RaiseError 没有引发足够的错误?
为什么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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只能引发用户消息,系统消息只能由引擎引发:
因此您无法引发原始
@ErrorMessage
,而必须引发新 错误代码。此外,您的 catch 块是不正确的,因为它假设一个不可提交的事务。情况并非总是如此,您必须检查
XACT_STATE()
在决定交易是否注定失败之前。在许多情况下,错误处理可以继续事务。请参阅异常处理和嵌套事务。You can only raise user messages, system messages can only be raised by the engine:
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.首先,查看 Remus 关于嵌套事务中的错误处理的文章,以便您了解全部内容。
然后,尝试将错误级别强制为 16。此外,在重新抛出错误时将原始错误信息嵌入到错误消息中,这样就不会丢失该信息:
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: