[英]How to find what type of exception occur from SQL Server Try Catch while executing stored procedure
I am executing a stored procedure which has some code inside it and I put it inside a try catch block like 我正在执行其中包含一些代码的存储过程,并将其放入try catch块中,例如
BEGIN Transaction Tran1
BEGIN Try
//Stored procedure code
COMMIT TRANSACTION Tran1
END Try
BEGIN Catch
ROLLBACK TRANSACTION Tran1
raiserror('Cannot commit transaction', 16, 1, @@error);
return;
END Catch
When any error occurred it goes to the Catch
block and raises the error which is a user defined error but it does not raise what type of error actually occurred. 当发生任何错误时,它会转到
Catch
块并引发错误(这是用户定义的错误),但不会引发实际发生的错误类型。 So how could I get what type of error that really occurred as we do in C# while executing it from front end like 因此,当从前端执行该错误时,如何像在C#中那样真正发生什么类型的错误?
Try
{
//Some code
}
Catch(Exception ex)
{
MessageBox.Show(ex.message);
}
Please provide me solution for this 请为此提供解决方案
The error raised is catchable as a SqlException
引发的错误是开捕为
SqlException
eg 例如
catch (SqlException sqlEx)
{
if (sqlEx.Number == 50000)
{
//
}
}
You can use the msg_id
flavour of RAISERROR
to customize the Number in SQL, or better still, use THROW
您可以使用
RAISERROR
的msg_id
在SQL中自定义数字,或者更好的是,使用THROW
You can try 你可以试试
BEGIN Transaction Tran1
BEGIN Try
//Stored procedure code
COMMIT TRANSACTION Tran1
END Try
BEGIN Catch
ROLLBACK TRANSACTION Tran1
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() as ErrorState,
ERROR_PROCEDURE() as ErrorProcedure,
ERROR_LINE() as ErrorLine,
ERROR_MESSAGE() as ErrorMessage;
END CATCH
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.