Solved

THROW

Posted on 2014-04-14
3
209 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query. 10 43
SQL query to summarize items per month 5 24
Scheduling Jobs for Execution: 4 14
Sql query 34 17
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

15 Experts available now in Live!

Get 1:1 Help Now