Solved

THROW

Posted on 2014-04-14
3
213 Views
Last Modified: 2014-04-17
Please experts,

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

BEGIN TRY
      --do something wrong
      SELECT 100/0 AS 'Problem';
END TRY
BEGIN CATCH
      PRINT 'Code inside CATCH is beginning'
      PRINT 'Error: ' + CAST(ERROR_NUMBER() AS VARCHAR(255));
      THROW;
END CATCH
0
Comment
Question by:enrique_aeo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 83 total points
ID: 39999061
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.
0
 
LVL 45

Accepted Solution

by:
Kent Olsen earned 84 total points
ID: 39999073
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.
0
 
LVL 11

Assisted Solution

by:John_Vidmar
John_Vidmar earned 83 total points
ID: 40004756
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.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Server 2012 r2 - Varible Table 3 39
why sql server only update some statistics in the database ? 3 40
SQL trigger 5 32
Update one rows based on previous row 5 28
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question