Solved

THROW

Posted on 2014-04-14
3
211 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
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:
Kdo 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now