[英]Raiserror in procedure (SQL Server 2014) does not catch client side(c#)
Stored procedure Catch block code. 存储过程捕获块代码。
alter PROCEDURE [dbo].[TESTError]
(
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
SELECT 5/0
END TRY
BEGIN CATCH
DECLARE @ErrorNumber INT
SELECT @ErrorNumber = ERROR_NUMBER()
RAISERROR
(N'The error code is: %d',
16, -- Severity.
1, -- State.
@ErrorNumber,
'');
END CATCH
END
The above stored procedure throw and show error when runs using SSMS. 使用SSMS运行时,以上存储过程引发并显示错误。
.Net Client code only exception portion. .Net客户端代码仅例外部分。
catch (SqlException ex)
{
string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
}
When application called stored procedure it does not catch in exception block. 当应用程序调用存储过程时,它不会捕获异常块。
Any kind of hint or idea would help me. 任何暗示或想法都会对我有所帮助。
catch (SqlException ex)
{
string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
}
What are you expecting to happen, even if this catch-block was entered? 即使输入了该捕获块,您期望发生什么? A string variable would get a value which is gone the next micro second...
字符串变量将获得一个值,该值将在下一毫秒内消失。
Anyway: Whenever you deal with specialised (typed) catch blocks, you should have a good reason not to have a general catch-block additionally (as last of your specialised ones): 无论如何:每当您处理专用(类型化)捕获块时,您都应该有充分的理由不额外使用通用捕获块(这是您专用的捕获块的最后一项):
catch (System.Exception sysex)
{
string msg = string.Format("Error number: {0} / Message: {1}", sysex.Number, sysex.Message);
//Do something with this information!
}
Or just 要不就
catch{throw;}
This depends on your needs and tools / habits of debugging... 这取决于您的需求和工具/调试习惯...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.