THROW replaces RAISERROR in SQL Server and it’s the preferred method going forward to raise error messages and push them to the calling code. THROW is actually a lot more intuitive than RAISERROR as far as how your code responds to it.
THROW was created as a means to more closely match error handling code in current programming languages. RAISERROR has been around since 1998, first released in SQL Server 7.0. THROW was first included in SQL Server 2012.
RAISERROR is still included in current versions of SQL Server, but there are notable differences between how the SQL flows when RAISERROR is called vs. when THROW is called. Specifically, RAISERROR does not stop the execution of the current procedure, whereas THROW always stops the current procedure and passes the error up to the calling procedure.
THROW like RAISERROR allows you to “raise” custom errors, but it immediately stops the current process and returns the error to the calling procedure.
THROW allows you to do pretty much everything that RAISERROR allowed, with one notable exception: THROW does not allow you to raise SQL Server system errors. But if you need to throw a system error, you can still do it by first using RAISERROR in a TRY BLOCK and using THROW in the corresponding CATCH block to throw the system error.
–The following statement results in an error because THROW can’t throw system exceptions:
THROW 40655, ‘Database master cannot be restored.’, 1;
Msg 35100, Level 16, State 10, Line 2
Error number 40655 in the THROW statement is outside the valid range. Specify an error number in the valid range of 50000 to 2147483647.
But you can work around this limitation of THROW of not being able to throw system exceptions by using it in conjunction with RAISERROR within a TRY/CATCH block:
–Throw system exception by using RAISERROR first in a TRY/CATCH block.
PRINT ‘Begin Try’;
RAISERROR (40655, 16, 1);
PRINT ‘End Try’;
PRINT ‘Begin Catch / Before Throwing Error’;
PRINT ‘End Catch / After Throwing Error’;
PRINT ‘After Try/Catch’;
Begin Catch / Before Throwing Error
Msg 40655, Level 16, State 1, Line 3
Database ‘master’ cannot be restored.
As you can see in the results, RAISERROR does not stop the process flow but does allow you to raise the system errors and THROW does stop the process flow and allows you to throw system errors you raise with RAISERROR to the calling process. Using the two statements together allows you to have full control of the error messages and when they are returned to the calling procedure.