{"id":4396,"date":"2025-02-25T18:13:54","date_gmt":"2025-02-25T21:13:54","guid":{"rendered":"https:\/\/powertuning.com.br\/?p=4396"},"modified":"2025-02-27T19:16:15","modified_gmt":"2025-02-27T22:16:15","slug":"poweralerts-tratamento-de-erros","status":"publish","type":"post","link":"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/","title":{"rendered":"[PowerAlerts] Tratamento de Erros"},"content":{"rendered":"<p>Fala, pessoal. Beleza?<\/p>\n<p>Na \u00faltima semana mostrei um caso onde o <strong>Power Alerts<\/strong> ajudou um cliente a identificar um problema na aplica\u00e7\u00e3o atrav\u00e9s do alerta <strong>[Database Errors]<\/strong>.<\/p>\n<p>Hoje vou explorar um pouco mais sobre esse tema de tratamentos de erros e mostrar melhores pr\u00e1ticas e situa\u00e7\u00f5es onde, ao utilizar o tratamento de erros, pode ser um problema para nossos c\u00f3digos.<\/p>\n<p>Bom, sem mais delongas vamos ao que interessa.<\/p>\n<p>Inicialmente, temos algumas formas de tratar erros no SQL Server. Os mais comuns de vermos s\u00e3o <strong>&#8220;@@ERROR&#8221;<\/strong>, <strong>RAISERROR<\/strong> e <strong>&#8220;Try Catch&#8221;<\/strong>. Iremos focar nesse \u00faltimo.<\/p>\n<p>Para deixar o mais did\u00e1tico poss\u00edvel, tentarei utilizar os exemplos mais curtos e diretos que conseguir, focando no problema e solu\u00e7\u00e3o e n\u00e3o no c\u00f3digo em si. Beleza?<\/p>\n<h4><strong>Cen\u00e1rio:<\/strong><\/h4>\n<p>Contextualizando o cen\u00e1rio, temos um ambiente de um sistema de ERP, realizando uma venda, e um sistema de <strong>LOGISTICA<\/strong>, que ir\u00e1 receber essa venda e seguir com os devidos tr\u00e2mites.<\/p>\n<h4><strong>Fail Fast:<\/strong><\/h4>\n<p>Iniciaremos nossa parte t\u00e9cnica com uma boa pr\u00e1tica chamada <strong>&#8220;Fail Fast&#8221;<\/strong>. Essa pr\u00e1tica consiste basicamente em sair\/parar o mais r\u00e1pido poss\u00edvel. Portanto, caso saibamos previamente que algo n\u00e3o faz sentido para a aplica\u00e7\u00e3o, <strong>devemos trat\u00e1-la antes mesmo de iniciar qualquer procedimento<\/strong>.<\/p>\n<p>No nosso caso, iremos tratar a possibilidade da quantidade e valor para n\u00e3o serem zerados ou negativos pois, esses valores n\u00e3o fazem sentido para nossa aplica\u00e7\u00e3o e podem gerar problemas em todo o ciclo de vida do dado.<\/p>\n<p>O c\u00f3digo ficou assim:<\/p>\n<pre class=\"lang:tsql decode:true\"><strong>CREATE PROCEDURE dbo.stpInserirVendaItem (\r\n\t@ID_VENDA INT\r\n\t,@ID_ITEM INT = 0\r\n\t,@NOME_PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10)\r\n\t,@QTD_VENDIDA INT\r\n\t,@VALOR DECIMAL(9,2)\r\n)\r\nAS\r\nBEGIN\r\n\t\r\n\tIF @QTD_VENDIDA &lt;= 0 OR @VALOR &lt;= 0\r\n\tBEGIN\r\n\t\tRAISERROR('Quantidade ou Valor inv\u00e1lido!',16,0)\r\n\t\tRETURN\r\n\tEND\r\n\r\n\tIF @ID_ITEM = 0\r\n\tBEGIN\r\n\t\tSELECT @ID_ITEM = ISNULL(MAX(ID_ITEM),0) + 1 FROM VENDA_ITEM WHERE ID_VENDA = @ID_VENDA\r\n\t\t\r\n\t\tINSERT INTO dbo.VENDA_ITEM (ID_VENDA,ID_ITEM,NOME_PRODUTO, FINALIDADE_VENDA, QTD_ITEM,VALOR_ITEM,VALOR_TOTAL)\r\n\t\tSELECT @ID_VENDA, @ID_ITEM, @NOME_PRODUTO, @FINALIDADE_VENDA, @QTD_VENDIDA,@VALOR, @QTD_VENDIDA * @VALOR\r\n\tEND\r\n\tELSE\r\n\tBEGIN\r\n\t\tUPDATE VENDA_ITEM SET QTD_ITEM = @QTD_VENDIDA, VALOR_ITEM = @VALOR, VALOR_TOTAL = @QTD_VENDIDA * @VALOR\r\n\t\tWHERE ID_VENDA = @ID_VENDA\r\n\t\tAND ID_ITEM = @ID_ITEM\r\n\tEND\r\n\r\n\tUPDATE PRODUTO SET QTD_ESTOQUE = QTD_ESTOQUE - @QTD_VENDIDA WHERE NOME = @NOME_PRODUTO\r\n\r\nEND\r\nGO<\/strong><\/pre>\n<p>Ao cair na condi\u00e7\u00e3o <strong>&#8220;QTD_VENDIDA OR VALOR &lt;= 0&#8221;<\/strong>, \u00e9 retornada a mensagem de erro <strong>&#8220;Quantidade ou Valor inv\u00e1lido!&#8221;<\/strong> e encerra a execu\u00e7\u00e3o. O encerramento \u00e9 garantido pelo comando RETURN.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4400\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/01-fail-fast\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast.png\" data-orig-size=\"1104,250\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"01 &amp;#8211; Fail Fast\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast-1024x232.png\" class=\"wp-image-4400 aligncenter\" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast-1024x232.png\" alt=\"\" width=\"767\" height=\"174\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast-1024x232.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast-300x68.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast-768x174.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/01-Fail-Fast.png 1104w\" sizes=\"auto, (max-width: 767px) 100vw, 767px\" \/><\/p>\n<p>Apenas para t\u00edtulo de conhecimento, no SQL Server, mesmo que ocorram erros no decorrer do c\u00f3digo, <strong>a execu\u00e7\u00e3o ainda \u00e9 mantida at\u00e9 o fim do objeto<\/strong>. Veja o exemplo abaixo, onde ocorre um erro de severidade 16 e ainda sim o c\u00f3digo continua sendo executado. Ao executar um erro de severidade 20 a sess\u00e3o \u00e9 finalizada:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4399\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/00-erros\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros.png\" data-orig-size=\"839,475\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"00 &amp;#8211; Erros\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros.png\" class=\"aligncenter wp-image-4399 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros.png\" alt=\"\" width=\"534\" height=\"302\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros.png 839w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros-300x170.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/00-Erros-768x435.png 768w\" sizes=\"auto, (max-width: 534px) 100vw, 534px\" \/><\/p>\n<p>Ao ajustar a quantidade e executada novamente, a linha \u00e9 inserida com sucesso:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4401\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/02-executando-com-sucesso\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso.png\" data-orig-size=\"1096,334\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"02 &amp;#8211; Executando com sucesso\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso-1024x312.png\" class=\"aligncenter wp-image-4401 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso.png\" alt=\"\" width=\"697\" height=\"212\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso.png 1096w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso-300x91.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso-1024x312.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/02-Executando-com-sucesso-768x234.png 768w\" sizes=\"auto, (max-width: 697px) 100vw, 697px\" \/><\/p>\n<h4><strong>TRY CATCH:<\/strong><\/h4>\n<p>Perfeito. Agora recebemos um requisito solicitando que <strong>&#8220;ao inserir um item, deve-se mandar esse item para a separa\u00e7\u00e3o&#8221;<\/strong>.<\/p>\n<p>A entidade <strong>&#8220;Separa\u00e7\u00e3o&#8221;<\/strong> est\u00e1 no banco de dados da Logistica do servidor <strong>DS-SRV02<\/strong>. Devemos chamar a procedure <strong>stpInserirSeparacao<\/strong> para essa finalidade.<\/p>\n<pre class=\"lang:tsql decode:true \">CREATE PROCEDURE dbo.stpInserirSeparacao (\r\n\t@PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10) = NULL\r\n\t,@QUANTIDADE INT\r\n\t,@ID_VENDA INT\r\n\t,@ID_ITEM INT\r\n)\r\nAS\r\nBEGIN\r\n\t\r\n\tUPDATE SEPARACAO SET QUANTIDADE = @QUANTIDADE\r\n\tWHERE ID_VENDA = @ID_VENDA\r\n\tAND ID_ITEM = @ID_ITEM\r\n\r\n\tIF @@ROWCOUNT = 0\r\n\t\tINSERT INTO SEPARACAO (PRODUTO,FINALIDADE_VENDA,QUANTIDADE,ID_VENDA, ID_ITEM)\r\n\t\tSELECT @PRODUTO, ISNULL(@FINALIDADE_VENDA,'NAO ENVIADA'), @QUANTIDADE, @ID_VENDA, @ID_ITEM\r\n\r\nEND\r\nGO<\/pre>\n<p>E inclu\u00edda a chamada da procedure.<\/p>\n<pre class=\"lang:tsql decode:true\">EXEC LOGISTICA.dbo.stpInserirSeparacao @PRODUTO = @NOME_PRODUTO, @QUANTIDADE = @QTD_VENDIDA, @ID_VENDA = @ID_VENDA, @ID_ITEM = @ID_ITEM<\/pre>\n<p>At\u00e9 aqui tudo bem, nosso c\u00f3digo est\u00e1 rodando conforme esperado:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4402\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/03-chamada-da-proc-de-logistica\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-Chamada-da-proc-de-logistica.png\" data-orig-size=\"681,453\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"03 &amp;#8211; Chamada da proc de logistica\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-Chamada-da-proc-de-logistica.png\" class=\"aligncenter wp-image-4402 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-Chamada-da-proc-de-logistica.png\" alt=\"\" width=\"427\" height=\"284\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-Chamada-da-proc-de-logistica.png 681w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-Chamada-da-proc-de-logistica-300x200.png 300w\" sizes=\"auto, (max-width: 427px) 100vw, 427px\" \/><\/p>\n<p>Agora, foi implementado um novo campo na procedure chamado da log\u00edstica chamado <strong>&#8220;Restri\u00e7\u00e3o de venda&#8221;<\/strong>. Esse campo deve sobrepor o campo <strong>&#8220;Finalidade de Venda&#8221;<\/strong> quando informado.<\/p>\n<p>Para esse ajuste foi realizado a substitui\u00e7\u00e3o da fun\u00e7\u00e3o<strong> ISNULL<\/strong> para COALESCE:<\/p>\n<pre class=\"lang:tsql decode:true \">IF OBJECT_ID('dbo.stpInserirSeparacao') IS NOT NULL\r\n\tDROP PROCEDURE dbo.stpInserirSeparacao\r\nGO\r\n\r\nCREATE PROCEDURE dbo.stpInserirSeparacao (\r\n\t@PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10) = NULL\r\n\t,@QUANTIDADE INT\r\n\t,@ID_VENDA INT\r\n\t,@ID_ITEM INT\r\n\t,@RESTRICAO VARCHAR(10) = NULL\r\n)\r\nAS\r\nBEGIN\r\n\t\r\n\tUPDATE SEPARACAO SET QUANTIDADE = @QUANTIDADE\r\n\tWHERE ID_VENDA = @ID_VENDA\r\n\tAND ID_ITEM = @ID_ITEM\r\n\r\n\tIF @@ROWCOUNT = 0\r\n\t\tINSERT INTO SEPARACAO (PRODUTO,FINALIDADE_VENDA,QUANTIDADE,ID_VENDA, ID_ITEM)\r\n\t\tSELECT @PRODUTO, COALESCE(@RESTRICAO, @FINALIDADE_VENDA,'NAO ENVIADA'), @QUANTIDADE, @ID_VENDA, @ID_ITEM\r\n\r\nEND\r\nGO<\/pre>\n<p>Seguindo o fluxo no ERP, em teoria, nada deveria mudar pois o novo campo n\u00e3o \u00e9 um campo obrigat\u00f3rio.<br \/>\nAo fazer a chamada temos o erro:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4403\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/03-coalesce-x-isnull\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL.png\" data-orig-size=\"1032,405\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"03 &amp;#8211; COALESCE X ISNULL\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL-1024x402.png\" class=\"aligncenter wp-image-4403 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL-1024x402.png\" alt=\"\" width=\"742\" height=\"291\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL-1024x402.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL-300x118.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL-768x301.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/03-COALESCE-X-ISNULL.png 1032w\" sizes=\"auto, (max-width: 742px) 100vw, 742px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>&#8220;Msg 2628, Level 16, State 1, Procedure LOGISTICA.dbo.stpInserirSeparacao, Line 18 [Batch Start Line 268]<\/strong><br \/>\n<strong>String or binary data would be truncated in table &#8216;LOGISTICA.dbo.SEPARACAO&#8217;, column &#8216;FINALIDADE_VENDA&#8217;. Truncated value: &#8216;NAO ENVIAD&#8217;.&#8221;<\/strong><\/p>\n<p>Mas o que aconteceu aqui? Somente foi trocada a fun\u00e7\u00e3o de <strong>ISNULL<\/strong> para COALESCE que, em teoria, fazem a mesma coisa s\u00f3 com par\u00e2metros a mais.<\/p>\n<p>E ambas vari\u00e1veis tem o mesmo tamanho. N\u00e3o deveria ter dado o erro!!!<\/p>\n<p>Pois bem, o leitor mais atento j\u00e1 deve ter percebido o problema. O <strong>ISNULL<\/strong> utiliza o tipo de dado do seu primeiro par\u00e2metro, ou seja, <strong>VARCHAR(10)<\/strong> e com isso ele trunca o segundo valor e insere ele na tabela. Nos prints anteriores \u00e9 poss\u00edvel ver o texto &#8220;NAO ENVIAD&#8221; efetivado na tabela.<\/p>\n<p>J\u00e1 o <strong>COALESCE<\/strong> n\u00e3o tem essa valida\u00e7\u00e3o, e utiliza o pr\u00f3prio tipo de dado que estiver na vari\u00e1vel\/coluna fazendo com que o erro de truncate ocorra. Sabiam dessa? \ud83d\ude09<\/p>\n<p>Mas, e agora? N\u00e3o temos poder de ajustar a procedure da Log\u00edstica. Como podemos fazer para que nossa aplica\u00e7\u00e3o n\u00e3o d\u00ea erro e continue executando normalmente?<\/p>\n<p><strong>Para essa finalidade podemos utilizar o TRY CATCH.<\/strong><\/p>\n<pre class=\"lang:tsql decode:true\">IF OBJECT_ID('dbo.stpInserirVendaItem') IS NOT NULL\r\n\tDROP PROCEDURE dbo.stpInserirVendaItem\r\nGO\r\n\r\nCREATE PROCEDURE dbo.stpInserirVendaItem (\r\n\t@ID_VENDA INT\r\n\t,@ID_ITEM INT = 0\r\n\t,@NOME_PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10)\r\n\t,@QTD_VENDIDA INT\r\n\t,@VALOR DECIMAL(9,2)\r\n)\r\nAS\r\nBEGIN\r\n\tBEGIN TRY \r\n\t\tIF @QTD_VENDIDA &lt;= 0 OR @VALOR &lt;= 0\r\n\t\tBEGIN\r\n\t\t\tRAISERROR('Quantidade ou Valor inv\u00e1lido!',16,0)\r\n\t\t\tRETURN\r\n\t\tEND\r\n\r\n\t\tIF @ID_ITEM = 0\r\n\t\tBEGIN\r\n\t\t\tSELECT @ID_ITEM = ISNULL(MAX(ID_ITEM),0) + 1 FROM VENDA_ITEM WHERE ID_VENDA = @ID_VENDA\r\n\t\t\r\n\t\t\tINSERT INTO dbo.VENDA_ITEM (ID_VENDA,ID_ITEM,NOME_PRODUTO, FINALIDADE_VENDA, QTD_ITEM,VALOR_ITEM,VALOR_TOTAL)\r\n\t\t\tSELECT @ID_VENDA, @ID_ITEM, @NOME_PRODUTO, @FINALIDADE_VENDA, @QTD_VENDIDA,@VALOR, @QTD_VENDIDA * @VALOR\r\n\t\tEND\r\n\t\tELSE\r\n\t\tBEGIN\r\n\t\t\tUPDATE VENDA_ITEM SET QTD_ITEM = @QTD_VENDIDA, VALOR_ITEM = @VALOR, VALOR_TOTAL = @QTD_VENDIDA * @VALOR\r\n\t\t\tWHERE ID_VENDA = @ID_VENDA\r\n\t\t\tAND ID_ITEM = @ID_ITEM\r\n\t\tEND\r\n\r\n\t\tEXEC [DS-SRV02].LOGISTICA.dbo.stpInserirSeparacao @PRODUTO = @NOME_PRODUTO, @QUANTIDADE = @QTD_VENDIDA\r\n\t\t\t, @ID_VENDA = @ID_VENDA, @ID_ITEM = @ID_ITEM\r\n\tEND TRY\r\n\tBEGIN CATCH\r\n\t\tPRINT ERROR_MESSAGE()\r\n\tEND CATCH\r\n\r\n\tUPDATE PRODUTO SET QTD_ESTOQUE = QTD_ESTOQUE - @QTD_VENDIDA WHERE NOME = @NOME_PRODUTO\r\n\r\nEND\r\nGO<\/pre>\n<p>E ao executarmos:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4404\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/04-try-catch-ainda-com-erro\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro.png\" data-orig-size=\"1007,379\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"04 &amp;#8211; Try Catch, ainda com erro\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro.png\" class=\"aligncenter wp-image-4404 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro.png\" alt=\"\" width=\"709\" height=\"267\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro.png 1007w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro-300x113.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-Try-Catch-ainda-com-erro-768x289.png 768w\" sizes=\"auto, (max-width: 709px) 100vw, 709px\" \/><\/p>\n<p>Continua com erro&#8230; E aqui vem o segundo ponto importante: nem todo erro \u00e9 tratado dentro do <strong>TRY&#8230;CATCH<\/strong>.<\/p>\n<p>Certo, a Logistica implementou o <strong>TRY CATCH emergencial<\/strong> do lado dela:<\/p>\n<pre class=\"lang:tsql decode:true \">IF OBJECT_ID('dbo.stpInserirSeparacao') IS NOT NULL\r\n\tDROP PROCEDURE dbo.stpInserirSeparacao\r\nGO\r\n\r\nCREATE PROCEDURE dbo.stpInserirSeparacao (\r\n\t@PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10) = NULL\r\n\t,@QUANTIDADE INT\r\n\t,@ID_VENDA INT\r\n\t,@ID_ITEM INT\r\n\t,@RESTRICAO VARCHAR(10) = NULL\r\n)\r\nAS\r\nBEGIN\r\n\t\r\n\tBEGIN TRY \r\n\t\tUPDATE SEPARACAO SET QUANTIDADE = @QUANTIDADE\r\n\t\tWHERE ID_VENDA = @ID_VENDA\r\n\t\tAND ID_ITEM = @ID_ITEM\r\n\r\n\t\tIF @@ROWCOUNT = 0\r\n\t\t\tINSERT INTO SEPARACAO (PRODUTO,FINALIDADE_VENDA,QUANTIDADE,ID_VENDA, ID_ITEM)\r\n\t\t\tSELECT @PRODUTO, COALESCE(@RESTRICAO,@FINALIDADE_VENDA,'NAO ENVIADA'), @QUANTIDADE, @ID_VENDA, @ID_ITEM\r\n\tEND TRY\r\n\tBEGIN CATCH\r\n\t\tPRINT ERROR_MESSAGE()\r\n\tEND CATCH\r\n\r\nEND\r\nGO<\/pre>\n<p>Agora sim, executamos com sucesso. Apesar n\u00e3o ter enviado nenhum item para a Separa\u00e7\u00e3o, pelo menos n\u00e3o paramos as vendas. Caso isso n\u00e3o fosse poss\u00edvel ter\u00edamos que comentar esse trecho, ou algo do tipo.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4405\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/04-chamada-com-sucesso-pos-try-catch\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch.png\" data-orig-size=\"1059,634\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"04 &amp;#8211; chamada com sucesso p\u00f3s Try Catch\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch-1024x613.png\" class=\"aligncenter wp-image-4405 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch-1024x613.png\" alt=\"\" width=\"637\" height=\"381\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch-1024x613.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch-300x180.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch-768x460.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/04-chamada-com-sucesso-pos-Try-Catch.png 1059w\" sizes=\"auto, (max-width: 637px) 100vw, 637px\" \/><\/p>\n<p>Mas, e se eu enviar um produto negativo conforme criamos a primeira valida\u00e7\u00e3o. O que vai acontecer?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4406\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/05-problema-com-o-try-catch\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch.png\" data-orig-size=\"1036,612\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"05 &amp;#8211; Problema com o Try Catch\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch-1024x605.png\" class=\"aligncenter wp-image-4406 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch-1024x605.png\" alt=\"\" width=\"677\" height=\"400\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch-1024x605.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch-300x177.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch-768x454.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/05-Problema-com-o-Try-Catch.png 1036w\" sizes=\"auto, (max-width: 677px) 100vw, 677px\" \/><\/p>\n<p>N\u00e3o deu erro conforme tinhamos visto no primeiro exemplo. E, para piorar, ele ainda somou a quantidade negativa deixando meu estoque errado.<\/p>\n<p>A utiliza\u00e7\u00e3o do <strong>TRY CATCH<\/strong> mudou o comportamento do nosso primeiro tratamento. Lembra que a premissa era validar e o return seria o respons\u00e1vel por finalizar a execu\u00e7\u00e3o? Ao executar o comando <strong>RAISERROR<\/strong>, o c\u00f3digo foi diretamente para o <strong>CATCH<\/strong>, n\u00e3o executando o <strong>RETURN<\/strong>. Importante destacar que eu finalizei o bloco <strong>TRY CATCH<\/strong> antes do <strong>&#8220;UPDATE PRODUTO&#8221;<\/strong>. Isso foi proposital para fins did\u00e1tico, para mostrar o quanto \u00e9 sens\u00edvel essa abordagem e importante contemplar o bloco correto.<\/p>\n<p>Beleza, ent\u00e3o vamos fazer a corre\u00e7\u00e3o deixando somente no local desejado que \u00e9 na chamada da Log\u00edstica:<\/p>\n<pre class=\"lang:tsql decode:true\">IF OBJECT_ID('dbo.stpInserirVendaItem') IS NOT NULL\r\n\tDROP PROCEDURE dbo.stpInserirVendaItem\r\nGO\r\n\r\nCREATE PROCEDURE dbo.stpInserirVendaItem (\r\n\t@ID_VENDA INT\r\n\t,@ID_ITEM INT = 0\r\n\t,@NOME_PRODUTO VARCHAR(100)\r\n\t,@FINALIDADE_VENDA VARCHAR(10)\r\n\t,@QTD_VENDIDA INT\r\n\t,@VALOR DECIMAL(9,2)\r\n)\r\nAS\r\nBEGIN\r\n\tIF @QTD_VENDIDA &lt;= 0 OR @VALOR &lt;= 0\r\n\tBEGIN\r\n\t\tRAISERROR('Quantidade ou Valor inv\u00e1lido!',16,0)\r\n\t\tRETURN\r\n\tEND\r\n\r\n\tIF @ID_ITEM = 0\r\n\tBEGIN\r\n\t\tSELECT @ID_ITEM = ISNULL(MAX(ID_ITEM),0) + 1 FROM VENDA_ITEM WHERE ID_VENDA = @ID_VENDA\r\n\t\t\r\n\t\tINSERT INTO dbo.VENDA_ITEM (ID_VENDA,ID_ITEM,NOME_PRODUTO, FINALIDADE_VENDA, QTD_ITEM,VALOR_ITEM,VALOR_TOTAL)\r\n\t\tSELECT @ID_VENDA, @ID_ITEM, @NOME_PRODUTO, @FINALIDADE_VENDA, @QTD_VENDIDA,@VALOR, @QTD_VENDIDA * @VALOR\r\n\tEND\r\n\tELSE\r\n\tBEGIN\r\n\t\tUPDATE VENDA_ITEM SET QTD_ITEM = @QTD_VENDIDA, VALOR_ITEM = @VALOR, VALOR_TOTAL = @QTD_VENDIDA * @VALOR\r\n\t\tWHERE ID_VENDA = @ID_VENDA\r\n\t\tAND ID_ITEM = @ID_ITEM\r\n\tEND\r\n\r\n\tBEGIN TRY \r\n\t\tEXEC [DS-SRV02].LOGISTICA.dbo.stpInserirSeparacao @PRODUTO = @NOME_PRODUTO, @QUANTIDADE = @QTD_VENDIDA\r\n\t\t\t, @ID_VENDA = @ID_VENDA, @ID_ITEM = @ID_ITEM\r\n\tEND TRY\r\n\tBEGIN CATCH\r\n\t\tDECLARE @MSG VARCHAR(MAX) = ERROR_MESSAGE()\r\n\t\tRAISERROR(@MSG,16,0)\r\n\tEND CATCH\r\n\r\n\tUPDATE PRODUTO SET QTD_ESTOQUE = QTD_ESTOQUE - @QTD_VENDIDA WHERE NOME = @NOME_PRODUTO\r\n\r\nEND\r\nGO<\/pre>\n<p>Agora voltamos ter o comportamento desejado:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4408\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/07-mostrando-o-bloco\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco.png\" data-orig-size=\"779,267\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"07 &amp;#8211; Mostrando o Bloco\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco.png\" class=\"aligncenter wp-image-4408 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco.png\" alt=\"\" width=\"651\" height=\"223\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco.png 779w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco-300x103.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/07-Mostrando-o-Bloco-768x263.png 768w\" sizes=\"auto, (max-width: 651px) 100vw, 651px\" \/><\/p>\n<h4><strong>Transa\u00e7\u00e3o:<\/strong><\/h4>\n<p>E para finalizar nossa s\u00e9rie de altera\u00e7\u00f5es, vamos para o \u00faltimo escopo (prometo, fica ai, est\u00e1 acabando).<\/p>\n<p>Foi nos solicitados que precisamos garantir o <strong>ACID<\/strong> dessa execu\u00e7\u00e3o. Para isso teremos que criar uma transa\u00e7\u00e3o e <strong>&#8220;commita-la&#8221;<\/strong> ao final do processo caso haja sucesso e dar o rollback caso haja erro.<\/p>\n<p>Iremos utilizar o BEGIN TRAN no \u00ednicio do processo e o commit ao final da procedure para esse objetivo.<\/p>\n<p>Vamos executar nosso c\u00f3digo:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4409\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/08-problema-no-dtc\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC.png\" data-orig-size=\"1247,480\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"08 &amp;#8211; Problema no DTC\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC-1024x394.png\" class=\"aligncenter wp-image-4409 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC-1024x394.png\" alt=\"\" width=\"666\" height=\"256\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC-1024x394.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC-300x115.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC-768x296.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/08-Problema-no-DTC.png 1247w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/p>\n<p>Tivemos problema com o <strong>DTC<\/strong> n\u00e3o estar habilitado. Para um breve resumo, o <strong>DTC<\/strong> \u00e9 o componente respons\u00e1vel por garantir as transa\u00e7\u00f5es em servidores diferentes. \u00c9 atrav\u00e9s dele que a inst\u00e2ncia A vai enviar algo e receber a devolutiva do OK. Para mais conhecimento, vejam o curso do <a href=\"https:\/\/cursos.powertuning.com.br\/course\/fundamentos-de-windows-para-dba-sql-server\">Rodrigo<\/a>.<\/p>\n<p>Corrigido o DTC, vou executar e:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4413\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/09-executando-com-dtc-2\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1.png\" data-orig-size=\"880,233\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"09 &amp;#8211; Executando com DTC\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1.png\" class=\"aligncenter wp-image-4413 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1.png\" alt=\"\" width=\"627\" height=\"166\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1.png 880w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1-300x79.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/09-Executando-com-DTC-1-768x203.png 768w\" sizes=\"auto, (max-width: 627px) 100vw, 627px\" \/><\/p>\n<p><strong>&#8220;The Microsoft Distributed Transaction Coordinator (MS DTC) has cancelled the distributed transaction.&#8221;<\/strong><\/p>\n<p>Que erro \u00e9 esse??? E se executar sem o DTC, o que acontece?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4412\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/10-executando-sem-dtc\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC.png\" data-orig-size=\"1043,278\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"10 &amp;#8211; Executando sem DTC\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC-1024x273.png\" class=\"aligncenter wp-image-4412 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC-1024x273.png\" alt=\"\" width=\"670\" height=\"179\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC-1024x273.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC-300x80.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC-768x205.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/10-Executando-sem-DTC.png 1043w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/p>\n<p>O que aconteceu aqui?<\/p>\n<p>Bom, quando encapsulamos o bloco try catch com a transa\u00e7\u00e3o &#8220;<strong>DTC&#8221;<\/strong> e o erro estoura do outro lado, recebemos apenas a informa\u00e7\u00e3o de que a transa\u00e7\u00e3o foi encerrada.<\/p>\n<p>O motivo do erro? S\u00f3 por aqui n\u00e3o saber\u00edamos. Mas, voc\u00ea meu caro leitor, <strong>sabe que o Power Alerts aqui para te ajudar<\/strong>, n\u00e9?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4414\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/power-alerts-erros-srv02\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02.png\" data-orig-size=\"1615,473\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Power Alerts &amp;#8211; Erros SRV02\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02-1024x300.png\" class=\"aligncenter wp-image-4414 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02.png\" alt=\"\" width=\"764\" height=\"224\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02.png 1615w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02-300x88.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02-1024x300.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02-768x225.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Power-Alerts-Erros-SRV02-1536x450.png 1536w\" sizes=\"auto, (max-width: 764px) 100vw, 764px\" \/><\/p>\n<p>Olha l\u00e1, quem est\u00e1 logado mostrando a mensagem de erro, o comando do erro e o objeto respons\u00e1vel. Por mais que n\u00e3o ter\u00edamos essa informa\u00e7\u00e3o no momento da execu\u00e7\u00e3o, <strong>o Power Alerts est\u00e1 ai para nos apoiar e identificar esses problemas<\/strong>. E qual a vis\u00e3o que temos do servidor 01?<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4415\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/servidor01\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01.png\" data-orig-size=\"1787,183\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Servidor01\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01-1024x105.png\" class=\"aligncenter wp-image-4415 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01.png\" alt=\"\" width=\"869\" height=\"89\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01.png 1787w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01-300x31.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01-1024x105.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01-768x79.png 768w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Servidor01-1536x157.png 1536w\" sizes=\"auto, (max-width: 869px) 100vw, 869px\" \/><\/p>\n<p>Olha que massa essa vis\u00e3o. Mostrando o caminho do erro.<\/p>\n<p>E j\u00e1 que voc\u00ea chegou at\u00e9 aqui, <strong>olha o b\u00f4nus de utilizar o Power Alerts para analisar o seus erros<\/strong>:<\/p>\n<pre class=\"lang:tsql decode:true \">CREATE OR ALTER PROCEDURE stpInception_erro \r\nAS\r\n\tSELECT 1\/0\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_010\r\nAS\r\n\tEXEC stpInception_erro\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_009\r\nAS\r\n\tEXEC stpInception_010\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_008\r\nAS\r\n\tEXEC stpInception_009\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_007\r\nAS\r\n\tEXEC stpInception_008\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_006\r\nAS\r\n\tEXEC stpInception_007\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_005\r\nAS\r\n\tEXEC stpInception_006\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_004\r\nAS\r\n\tEXEC stpInception_005\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_003\r\nAS\r\n\tEXEC stpInception_004\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_002\r\nAS\r\n\tEXEC stpInception_003\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception_001\r\nAS\r\n\tEXEC stpInception_002\r\nGO\r\n\r\nCREATE OR ALTER PROCEDURE stpInception\r\nAS\r\n\tEXEC stpInception_001\r\nGO\r\n\r\nEXEC stpInception<\/pre>\n<p>Retorno do Power Alerts:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"4416\" data-permalink=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/caminho-do-erro\/\" data-orig-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro.png\" data-orig-size=\"1124,351\" data-comments-opened=\"0\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"Caminho do Erro\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro-1024x320.png\" class=\"aligncenter wp-image-4416 \" src=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro.png\" alt=\"\" width=\"753\" height=\"235\" srcset=\"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro.png 1124w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro-300x94.png 300w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro-1024x320.png 1024w, https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Caminho-do-Erro-768x240.png 768w\" sizes=\"auto, (max-width: 753px) 100vw, 753px\" \/><\/p>\n<p>Simplesmente o caminho das 13 procedures at\u00e9 o erro. Imagina essa facilidade para achar o ponto do erro dentro de v\u00e1rias e v\u00e1rias procedures.<\/p>\n<p>Isso \u00e9 puro ouro.<\/p>\n<p>Bom, \u00e9 isso pessoal.<\/p>\n<p>Espero que tenham gostado e nos vemos nas pr\u00f3ximas.<\/p>\n<p><strong>E lembrem-se, o Power Alerts est\u00e1 aqui para ajudar voc\u00eas<\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Fala, pessoal. Beleza? Na \u00faltima semana mostrei um caso onde o Power Alerts ajudou um cliente a identificar um problema na aplica\u00e7\u00e3o atrav\u00e9s do alerta [Database Errors]. Hoje vou explorar um pouco mais sobre esse tema de tratamentos de erros e mostrar melhores pr\u00e1ticas e situa\u00e7\u00f5es onde, ao utilizar o tratamento de erros, pode ser&hellip; <br \/> <a class=\"read-more\" href=\"https:\/\/powertuning.com.br\/blog\/poweralerts-tratamento-de-erros\/\">Leia mais<\/a><\/p>\n","protected":false},"author":20,"featured_media":4426,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"content-type":"","_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[176,138,63],"tags":[13,379,384,386,381,378,380,382,383,385,141,352,377,158,35,375,376],"class_list":["post-4396","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-banco-de-dados","category-power-alerts","category-sql-server","tag-banco-de-dados","tag-coalesce","tag-debug-sql","tag-desenvolvimento-de-software","tag-erp","tag-fail-fast","tag-isnull","tag-logistica","tag-melhores-praticas","tag-mensagens-de-erro","tag-power-alerts","tag-procedimentos-armazenados","tag-raiserror","tag-sql","tag-sql-server","tag-tratamento-de-erros","tag-try-catch"],"jetpack_featured_media_url":"https:\/\/powertuning.com.br\/blog\/wp-content\/uploads\/2025\/02\/Novidade-no-blog-2025-1.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/4396","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/comments?post=4396"}],"version-history":[{"count":9,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/4396\/revisions"}],"predecessor-version":[{"id":4430,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/posts\/4396\/revisions\/4430"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/media\/4426"}],"wp:attachment":[{"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/media?parent=4396"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/categories?post=4396"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/powertuning.com.br\/blog\/wp-json\/wp\/v2\/tags?post=4396"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}