THROW statement TSQL 2012

Today,

We will go through the THROW error handling technique which Microsoft is introducing in SQL server 2012

at first we will be looking at RAISERROR () and will compare this with THROW and find out the difference and advantages , disadvantages

Highlights of RAISERROR()

  • RAISERROR allows you to throw an error based on either an error number or a message, and you can define the severity level and state of that error:
    1
    2
    3
    RAISERROR(50001, 16, 1);
    --or
    RAISERROR('Ooops', 16, 1);
  • If you call RAISERROR with an error number, that error number has to exist in sys.messages.
  • You can use error numbers between 13001 and 2147483647 (it cannot be 50000) with RAISERROR.

RAISERROR can return either:

  • A user-defined error message that has been created using the sp_addmessage system stored procedure. These are messages with a message number greater than 50000 that can be viewed in the sys.messages catalog view.
  • A message string specified in the RAISERROR statement.

RAISERROR can also:

  • Assign a specific error number, severity, and state.
  • Request that the error be logged in the Database Engine error log and the Microsoft Windows application log.
  • Substitute argument values into the message text, much like the C language printf_s function.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Drawbacks of RAISERROR()

  • One of the biggest draw back of the RAISERROR() is the in ability  of re throwing the error in catch block of TRY-CATCH  error handling technique introduced in SQL SERVER 2005

RAISERROR() in TRY-CATCH

 

BEGIN TRY
    -- RAISERROR with severity 11-19 will cause execution to
    -- jump to the CATCH block.
    RAISERROR ('Error raised in TRY block.', -- Message text.
               16, -- Severity.
               1 -- State.
               );
END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;
   we lost whole point of catching with a fair level of complexity in the catch block

XACT_ABORT

As every T-SQL programmer worth his (or her) salt should know, an exception does not roll back a transaction by default

I.e. the following code would cause two rows to be inserted in the table


when using raise error to throw the same error lets see what happens

keeping these things in mind Microsoft has introduced a new technique to replace the RAISERROR()

THROW SYNTAX THROW

[ { error_number | @local_variable }, { message | @local_variable },     { state | @local_variable } ] [ ; ] error_number

Is a constant or variable that represents the exception. error_number is int and must be greater than or equal to 50000 and less than or equal to 2147483647.

message

Is an string or variable that describes the exception. message is nvarchar(2048).

state

Is a constant or variable between 0 and 255 that indicates the state to associate with the message. state is tinyint.

  • The statement before the THROW statement must be followed by the semicolon (;) statement terminator.
  • If a TRY…CATCH construct is not available, the session is ended. The line number and procedure where the exception is raised are set. The severity is set to 16.
  • If the THROW statement is specified without parameters, it must appear inside a CATCH block. This causes the caught exception to be raised. Any error that occurs in a THROW statement causes the statement batch to be ended.

Three Main Differences Between RAISERROR and THROW

The following table lists differences between the RAISERROR and THROW statements.

RAISERROR statement THROW statement
If a msg_id is passed to RAISERROR, the ID must be defined in sys.messages. The error_number parameter does not have to be defined in sys.messages.
The msg_str parameter can contain printf formatting styles. The message parameter does not accept printf style formatting.
The severity parameter specifies the severity of the exception. There is no severity parameter. The exception severity is always set to 16.

as we can see this how error can be re thrown in try catch block One good feature that I like in RAISERROR is Custom formatting the error message as shown below the simple one with substitution arguments please use the below link http://msdn.microsoft.com/en-us/library/ms178592(v=SQL.110).aspx

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

References:

http://msdn.microsoft.com/en-us/library/ms186788(v=SQL.110).aspx

http://msdn.microsoft.com/en-us/library/ms177497(v=SQL.105).aspx

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

Advertisements

One thought on “THROW statement TSQL 2012

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s