Friday, 31 December 2010

SQL Server 2011 - THROW

Just a quick one, as 3 posts on New Year's Eve is probably a little excessive, but I have just found a cool new (and long awaited) feature in Denali. The ability to Throw an error.

You can use this in your custom error handling to throw a custom error message, like RAISERROR but also (and probably more usefully) you can use it in a CATCH block, to essentially re-throw the error that caused the CATCH block to kick in. For example...

...To produce the following output...
BEGIN TRY
     SELECT 1/0
END TRY
BEGIN CATCH
     THROW
END CATCH


...There you go, very simple, but every liuttle helps!
(0 row(s) affected)
Msg 8134, Level 16, State 1, Line 2
Divide by zero error encountered.


I am reliably informed that the final release will also include a FINALLY block, but that does not seem to work in CTP1, so I will post on that after I have had the chance to play with it.

HAPPY NEW YEAR!!!

No comments:

Post a Comment