Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

THROW

Posted on 2014-04-14
3
Medium Priority
?
222 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 66

Assisted Solution

by:Jim Horn
Jim Horn earned 332 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 46

Accepted Solution

by:
Kent Olsen earned 336 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 332 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

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 …
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Screencast - Getting to Know the Pipeline

610 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