sql-server - T-SQL TRY CATCH 的问题?

标签 sql-server tsql try-catch

我们目前正在使用 SQL 2005,我正在将旧的 Foxpro 系统迁移到由 SQL Server 支持的新 Web 应用程序。我在 T-SQL 中使用 TRY CATCH 进行事务处理,它似乎工作得很好。一位在职的其他程序员对此感到担忧,因为他说他听说过一些问题,其中标语并不总是能捕捉到错误。我已经把 sproc 打败了,不能让它失败(错过一个捕获),我在网上搜索的唯一问题是它不会返回错误号 < 5000 的正确错误号。有没有人遇到过其他任何问题T-SQL 中的 TRY CATCH 问题 - 特别是如果它错过了一个捕获?感谢您提供的任何意见。

最佳答案

TRY ... CATCH没有捕获所有可能的错误,但未捕获的错误在 BOL Errors Unaffected by a TRY…CATCH Construct 中有详细记录

TRY…CATCH constructs do not trap the following conditions:

  • Warnings or informational messages that have a severity of 10 or lower.
  • Errors that have a severity of 20 or higher that stop the SQL Server Database Engine task processing for the session. If an error occurs that has severity of 20 or higher and the database connection is not disrupted, TRY…CATCH will handle the error.
  • Attentions, such as client-interrupt requests or broken client connections.
  • When the session is ended by a system administrator by using the KILL statement.

The following types of errors are not handled by a CATCH block when they occur at the same level of execution as the TRY…CATCH construct:

  • Compile errors, such as syntax errors, that prevent a batch from running.
  • Errors that occur during statement-level recompilation, such as object name resolution errors that occur after compilation because of deferred name resolution.

These errors are returned to the level that ran the batch, stored procedure, or trigger.

关于sql-server - T-SQL TRY CATCH 的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6018555/

相关文章:

c# - 以编程方式生成sql代码

tsql - 什么时候不应该使用分号?

c# - try/catch + using,正确的语法

php - 如何在 PHP 和 MongoDB 中正确捕获异常

sql - 在 SQL Server 中实现审计表的建议?

sql-server - 全文索引未从 pdf 文件流返回任何结果

sql - 将 nvarchar 转换为日期时间

c# - 从 WPF 保存到 SQL Server 数据库

sql - 使用子查询提取随机值每次都会显示相同的值

typescript - 如何在TypeScript中尝试catch和finally语句?