Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Bye Bye to the RAISERROR Function?

By Daniel Calbimonte,

Introduction

If you review the Books Online, you will find the SQL Server 2012 documentation is recommending not to use RAISERROR for future code as it will be removed in future releases. It recommends to use the Throw statement instead.

Why is RAISERROR going to be removed? Well, there obvious answers:

  •  THROW is a standard for error handling. All the C, C#, java programmers will feel really comfortable with the THROW statement.
  •  There are some errors when RAISERROR is combined with the TRY, CATCH options, especially when we change the severity.

Getting started with the Throw statement

Let’s play with a simple example of the THROW usage:

THROW 50000, 'This is the error message.', 1;

Here 50000 is the error number and state is used as a reference to find the error in the code. 1 is the state of the error. The state can be a number from 0 to 255. It is used to know the location of the code where it fails. 

If you notice, it is similar to RAISERROR, but it does not include the severity.

RAISERROR (N'This is the error message',16,1);

By default, the Throw statement always has the severity of 16.

THROW USED IN A TRY CATCH SENTENCE

The following example is a typical try, catch sentence. In this example, we are forcing a divide by 0 operation. The @value1 is inside the TRY and the THROW will throw the error message expected.

BEGIN TRY
       declare @value1 int = 2/0
END TRY
BEGIN CATCH
       PRINT 'Error divide by 0';
       THROW
END CATCH

The error message displayed by the statement is:

Error divide by 0

Msg 8134, Level 16, State 1, Line 2
Divide by zero error encountered.

Disadvantages of the Throw statement.

  • Throw does not include the WITH LOG option included by RAISERROR which let us log information in the ERROR and application log.
  • We do not have now the nice printf options that we had in the past to manage variables.

Advantages of the Throw statement.

  • We do not need to use and handle the sys.messages view, add messages, remove messages.
  • It is a standard method to handle errors.
  • The function is shorter and uses less parameters.

Conclusion

The new SQL Server 2012 is adding a nice useful and standard statement to handle error messages. The old Raiserror will be replaced in the future with the new Throw option.  

References

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

THROW

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

Total article views: 1712 | Views in the last 30 days: 7
 
Related Articles
FORUM

Logging Raiserror log messages into a table

Logging Raiserror log messages into a table

FORUM

RAISERROR - not getting proper error message when calling nested procedures

Not able to get the proper error message and error number combination when using custom error messag...

FORUM

sp_replicationdboption throwing error

sp_replicationdboption throwing error

FORUM

Throw an error/exception from a function

Throw an error/exception from a function

Tags
catch    
raise    
throw    
try..catch    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones