{"id":164,"date":"2019-09-24T08:00:36","date_gmt":"2019-09-24T08:00:36","guid":{"rendered":"http:\/\/sumitjangid.com\/?p=164"},"modified":"2019-09-24T08:03:46","modified_gmt":"2019-09-24T08:03:46","slug":"throw","status":"publish","type":"post","link":"http:\/\/sumitjangid.com\/index.php\/2019\/09\/24\/throw\/","title":{"rendered":"Throw"},"content":{"rendered":"\n<h2>SQL Server&nbsp;<code>THROW<\/code>&nbsp;statement overview<\/h2>\n\n\n\n<p>The&nbsp;<code>THROW<\/code>&nbsp;statement raises an exception and transfers execution to a&nbsp;<code>CATCH<\/code>&nbsp;block of a&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-try-catch\/\">TRY CATCH<\/a><\/code>&nbsp;construct.<\/p>\n\n\n\n<p>The following illustrates the syntax of the&nbsp;<code>THROW<\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123<\/td><td>THROW [ error_number ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state ];<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this syntax:<\/p>\n\n\n\n<h3>error_number<\/h3>\n\n\n\n<p>The&nbsp;<code>error_number<\/code>&nbsp;is an integer that represents the exception. The&nbsp;<code>error_number<\/code>&nbsp;must be greater than&nbsp;<code>50,000<\/code>&nbsp;and less than or equal to&nbsp;<code>2,147,483,647<\/code>.<\/p>\n\n\n\n<h3>message<\/h3>\n\n\n\n<p>The&nbsp;<code>message<\/code>&nbsp;is a string of type&nbsp;<code>NVARCHAR(2048)<\/code>&nbsp;that describes the exception.<\/p>\n\n\n\n<h3>state<\/h3>\n\n\n\n<p>The&nbsp;<code>state<\/code>&nbsp;is a&nbsp;<code>TINYINT<\/code>&nbsp;with the value between 0 and 255. The&nbsp;<code>state<\/code>&nbsp;indicates the state associated with the message.<\/p>\n\n\n\n<p>If you don\u2019t specify any parameter for the&nbsp;<code>THROW<\/code>&nbsp;statement, you must place the&nbsp;<code>THROW<\/code>&nbsp;statement inside a&nbsp;<code>CATCH<\/code>&nbsp;block:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>BEGIN TRY&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; statements that may cause errors<\/em>END TRYBEGIN CATCH&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212; statement to handle errors <\/em>&nbsp;&nbsp;&nbsp;&nbsp;THROW;&nbsp;&nbsp; END CATCH<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this case, the&nbsp;<code>THROW<\/code>&nbsp;statement raises the error that was caught by the&nbsp;<code>CATCH<\/code>&nbsp;block.<\/p>\n\n\n\n<p>Note that the statement before the&nbsp;<code>THROW<\/code>&nbsp;statement must be terminated by a semicolon (;)<\/p>\n\n\n\n<h2>&nbsp;<code>THROW&nbsp;<\/code>vs.&nbsp;<code>RAISERROR<\/code><\/h2>\n\n\n\n<p>The following table illustrates the difference between the&nbsp;<code>THROW<\/code>&nbsp;statement and&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-raiserror\/\">RAISERROR<\/a><\/code>&nbsp;statement:<\/p>\n\n\n\n<table class=\"wp-block-table\"><thead><tr><th>RAISERROR<\/th><th>THROW<\/th><\/tr><\/thead><tbody><tr><td>The&nbsp;<code>message_id<\/code>&nbsp;that you pass to&nbsp;<code>RAISERROR<\/code>&nbsp;must be defined in&nbsp;<code>sys.messages<\/code>&nbsp;view.<\/td><td>The&nbsp;<em>error_number<\/em>&nbsp;parameter does not have to be defined in the&nbsp;<code>sys.messages<\/code>&nbsp;view.<\/td><\/tr><tr><td>The&nbsp;<em>message&nbsp;<\/em>parameter can contain&nbsp;<strong>printf<\/strong>&nbsp;formatting styles such as&nbsp;<code>%s<\/code>&nbsp;and&nbsp;<code>%d<\/code>.<\/td><td>The&nbsp;<em>message&nbsp;<\/em>parameter does not accept&nbsp;<strong>printf<\/strong>&nbsp;style formatting. Use&nbsp;<code>FORMATMESSAGE()<\/code>&nbsp;function to substitute parameters.<\/td><\/tr><tr><td>The&nbsp;<em>severity<\/em>&nbsp;parameter indicates the severity of the exception.<\/td><td>The severity of the exception is always set to 16.<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h2>SQL Server&nbsp;<code>THROW<\/code>&nbsp;statement examples<\/h2>\n\n\n\n<p>Let\u2019s take some examples of using the&nbsp;<code>THROW<\/code>&nbsp;statement to get a better understanding.<\/p>\n\n\n\n<h3>A) Using&nbsp;<code>THROW<\/code>&nbsp;statement to raise an exception<\/h3>\n\n\n\n<p>The following example uses the&nbsp;<code>THROW<\/code>&nbsp;statement to raise an exception:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1<\/td><td>THROW 50005, N&#8217;An error occurred&#8217;, 1;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>Msg 50005, Level 16, State 1, Line 1An error occurred<\/td><\/tr><\/tbody><\/table>\n\n\n\n<h3>B) Using&nbsp;<code>THROW<\/code>&nbsp;statement to rethrow an exception<\/h3>\n\n\n\n<p>First,&nbsp;<a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-create-table\/\">create a new table<\/a>&nbsp;<code>t1<\/code>&nbsp;for the demonstration:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>CREATE TABLE t1(&nbsp;&nbsp;&nbsp;&nbsp;id int primary key);GO<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Then, use the&nbsp;<code>THROW<\/code>&nbsp;statement without arguments in the&nbsp;<code>CATCH<\/code>&nbsp;block to rethrow the caught error:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456789<\/td><td>BEGIN TRY&nbsp;&nbsp;&nbsp;&nbsp;INSERT INTO t1(id) VALUES(1);&nbsp;&nbsp;&nbsp;&nbsp;<em>&#8212;&nbsp;&nbsp;cause error<\/em>&nbsp;&nbsp;&nbsp;&nbsp;INSERT INTO t1(id) VALUES(1);END TRYBEGIN CATCH&nbsp;&nbsp;&nbsp;&nbsp;PRINT(&#8216;Raise the caught error again&#8217;);&nbsp;&nbsp;&nbsp;&nbsp;THROW;END CATCH<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>123456<\/td><td>(1 row affected)&nbsp;(0 rows affected)Raise the caught error againMsg 2627, Level 14, State 1, Line 10Violation of PRIMARY KEY constraint &#8216;PK__t1__3213E83F906A55AA&#8217;. Cannot insert duplicate key in object &#8216;dbo.t1&#8217;. The duplicate key value is (1).<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>In this example, the first&nbsp;<code>INSERT<\/code>&nbsp;statement succeeded. However, the second one failed due to the primary key constraint. Therefore, the error was caught by the&nbsp;<code>CATCH<\/code>&nbsp;block was raised again by the&nbsp;<code>THROW<\/code>&nbsp;statement.<\/p>\n\n\n\n<h3>C) Using&nbsp;<code>THROW<\/code>&nbsp;statement to rethrow an exception<\/h3>\n\n\n\n<p>Unlike the&nbsp;<code><a href=\"http:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/sql-server-raiserror\/\">RAISERROR<\/a><\/code>&nbsp;statement, the&nbsp;<code>THROW<\/code>&nbsp;statement does not allow you to substitute parameters in the message text. Therefore, to mimic this function, you use the&nbsp;<code>FORMATMESSAGE()<\/code>&nbsp;function.<\/p>\n\n\n\n<p>The following statement adds a custom message to the&nbsp;<code>sys.messages<\/code>&nbsp;catalog view:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234567<\/td><td>EXEC sys.sp_addmessage &nbsp;&nbsp;&nbsp;&nbsp;@msgnum = 50010, &nbsp;&nbsp;&nbsp;&nbsp;@severity = 16, &nbsp;&nbsp;&nbsp;&nbsp;@msgtext =&nbsp;&nbsp;&nbsp;&nbsp;N&#8217;The order number %s cannot be deleted because it does not exist.&#8217;, &nbsp;&nbsp;&nbsp;&nbsp;@lang = &#8216;us_english&#8217;;&nbsp;&nbsp; GO<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>This statement uses the&nbsp;<code>message_id<\/code>&nbsp;50010 and replaces the&nbsp;<code>%s<\/code>&nbsp;placeholder by an order id \u20181001\u2019:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>1234<\/td><td>DECLARE @MessageText NVARCHAR(2048);SET @MessageText =&nbsp;&nbsp;FORMATMESSAGE(50010, N&#8217;1001&#8242;);&nbsp;&nbsp; &nbsp;THROW 50010, @MessageText, 1; <\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>Here is the output:<\/p>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td>12<\/td><td>Msg 50010, Level 16, State 1, Line 8The order number 1001 cannot be deleted because it does not exist.<\/td><\/tr><\/tbody><\/table>\n","protected":false},"excerpt":{"rendered":"<p>SQL Server&nbsp;THROW&nbsp;statement overview The&nbsp;THROW&nbsp;statement raises an exception and transfers execution to a&nbsp;CATCH&nbsp;block of a&nbsp;TRY CATCH&nbsp;construct. The following illustrates the syntax of the&nbsp;THROW&nbsp;statement: 123 THROW [ error_number ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message ,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state ]; In this syntax: error_number The&nbsp;error_number&nbsp;is an&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2,18,19],"tags":[],"_links":{"self":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/164"}],"collection":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/comments?post=164"}],"version-history":[{"count":1,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions"}],"predecessor-version":[{"id":165,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/posts\/164\/revisions\/165"}],"wp:attachment":[{"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/media?parent=164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/categories?post=164"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sumitjangid.com\/index.php\/wp-json\/wp\/v2\/tags?post=164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}