Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rethrow the same exception in SQL Server

I want to rethrow the same exception in SQL Server that has just occurred in my try block. I am able to throw same message but I want to throw same error.

BEGIN TRANSACTION     BEGIN TRY         INSERT INTO Tags.tblDomain (DomainName, SubDomainId, DomainCode, Description)             VALUES(@DomainName, @SubDomainId, @DomainCode, @Description)         COMMIT TRANSACTION     END TRY          BEGIN CATCH         declare @severity int;          declare @state int;          select @severity=error_severity(), @state=error_state();          RAISERROR(@@Error,@ErrorSeverity,@state);         ROLLBACK TRANSACTION     END CATCH 

RAISERROR(@@Error, @ErrorSeverity, @state);

This line will show error, but I want functionality something like that. This raises error with error number 50000, but I want the error number to be thrown that I am passing @@error,

I want to capture this error no at the frontend.

i.e.

catch (SqlException ex) {     if ex.number==2627     MessageBox.show("Duplicate value cannot be inserted"); } 

I want this functionality. which can't be achieved using raiseerror. I don't want to give custom error message at back end.

RAISEERROR should return below mentioned error when I pass ErrorNo to be thrown in catch

Msg 2627, Level 14, State 1, Procedure spOTest_DomainInsert, 

Line 14 Violation of UNIQUE KEY constraint 'UK_DomainCode'. Cannot insert duplicate key in object 'Tags.tblDomain'. The statement has been terminated.

EDIT:

What can be the drawback of not using try catch block if I want exception to be handled at frontend considering stored procedure contains multiple queries that need to be executed?

like image 370
Shantanu Gupta Avatar asked Mar 20 '10 00:03

Shantanu Gupta


People also ask

How do I create a custom exception in SQL?

Throw custom exception in SQL Server stored procedure Throwing a custom exception means you can cause an exception to encounter an error at any part of your code according to your logic. We use the THROW statement or the RAISERROR function for throwing a custom exception.

What is throw in SQL?

Throw is used to raises exception and transfers execution to a CATCH block in SQL server. Introduction. The THROW statement is introduced with SQL Server 2012. Throw is used to raise an exception and transfers execution to a CATCH block in SQL Server.

How do I raise an exception in SQL Server?

The following example shows how to use the THROW statement to raise the last thrown exception again. USE tempdb; GO CREATE TABLE dbo. TestRethrow ( ID INT PRIMARY KEY ); BEGIN TRY INSERT dbo. TestRethrow(ID) VALUES(1); -- Force error 2627, Violation of PRIMARY KEY constraint to be raised.

What is the difference between Raiserror and throw in SQL Server?

According to the Differences Between RAISERROR and THROW in Sql Server: Both RAISERROR and THROW statements are used to raise an error in Sql Server. The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just began with Sql Server 2012.


1 Answers

SQL 2012 introduces the throw statement:

http://msdn.microsoft.com/en-us/library/ee677615.aspx

If the THROW statement is specified without parameters, it must appear inside a CATCH block. This causes the caught exception to be raised.

BEGIN TRY     BEGIN TRANSACTION     ...     COMMIT TRANSACTION END TRY BEGIN CATCH     ROLLBACK TRANSACTION;     THROW END CATCH 
like image 197
Michael Avatar answered Sep 25 '22 05:09

Michael