Please experts,

what is the advantage of using the throw In this code?

      --do something wrong
      SELECT 100/0 AS 'Problem';
      PRINT 'Code inside CATCH is beginning'
      PRINT 'Error: ' + CAST(ERROR_NUMBER() AS VARCHAR(255));
Who is Participating?
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
The THROW in that handler passes control from the current function back to the default handler.  The program has issued the message(s) that it want to send and is now reverting to the next level handler, usually the system default handler which will likely abort the program.
Jim HornConnect With a Mentor Microsoft SQL Server Developer, Architect, and AuthorCommented:
It allows you to 'roll your own' error messages based on whatever business logic you desire, in addition to SQL Server's native error handling.

Since most Microsoft error messages are about as readable as Egyptian Sanskrit + code that would be in a Dan Brown novel, being able to write your own English sounding messages can be very attractive.
John_VidmarConnect With a Mentor Commented:
Without the try-catch, the error-severity could cause your SQL batch to terminate, which affects your transaction.  With the try-catch, the developer can control whether or not they want their SQL batch to terminate, and perhaps do some cleanup before terminating.

Including the THROW in a CATCH means you want to terminate, excluding it means keep going.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.