Guest User

Untitled

a guest
Oct 21st, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. -- STRUCTURED EXCEPTION HANDLING is where you create a structure to intercept and handle errors. This is often done with a TRY block that tries to do something, and then you have a CATCH block that catches the error and then handles the error in the case that something fails during the TRY block. Common CATCH block techniques are to put in code that logs what the error was, and/or tries to fix the error, and/or tries to re-THROW the error so that any application you're using can do it's own error handling (ie expose the error to SLQ Server so it can do it's normal error handling), or THROW a custom error message that says something like 'an error occurred but it's been taken care of and you're good'.
  2.  
  3. ---------------------------------------------------------------------------------------------------
  4. -- Simple TRY and CATCH example. CATCH block only runs if there's an exception in the TRY block. --
  5. ---------------------------------------------------------------------------------------------------
  6.  
  7. -- begins the TRY block
  8. BEGIN TRY
  9. UPDATE Production.Product
  10. -- statement that will intentially divide by zero and therefore create an error
  11. SET PRICE = Price / 0
  12. END TRY
  13. -- begins the CATCH block that will run if an error results from the TRY block
  14. BEGIN CATCH
  15. -- prints the following text
  16. PRINT 'The following error occurred';
  17. --Function that prints the error message encountered
  18. PRINT ERROR_MESSAGE();
  19. -- THROWs an error message
  20. THROW 50001, 'An error occurred', 0;
  21. END CATCH;
  22.  
  23. /*
  24.  
  25. Result from query above - error is THROWN to the user, but not exposed to the application:
  26.  
  27. (0 row(s) affected)
  28. The following error occurred:
  29. Divide by zero error encountered.
  30.  
  31. */
  32.  
  33. ------------------------------------------------------------------------------------------------------------------------------------------------------------
  34. -- Example that handles the error and then re-THROWs the error so it's exposed to SQL Server and SQL Server can then go through its usual error handling. --
  35. ------------------------------------------------------------------------------------------------------------------------------------------------------------
  36.  
  37. -- Begins TRY block
  38. BEGIN TRY
  39. UPDATE Production.Product2
  40. -- statement that will intentionally cause a divide by zero error
  41. SET StandardCost = ProductID / 0;
  42. END TRY
  43. -- Begins CATCH block which will execute if there's an error in TRY block
  44. BEGIN CATCH
  45. -- Prints 'The following error occurred:'
  46. PRINT 'The following error occurred:';
  47. -- Prints the error message encountered so user can see it.
  48. PRINT ERROR_MESSAGE();
  49. -- THROW by itself will THROW the currently outstanding error (rethrowing the error so the client application can get it and do any of it's own error handling - the client application will only see the error produced by the THROW, it will not see or handle the error message produced by the CATCH block).
  50. THROW;
  51. END CATCH;
  52.  
  53.  
  54. /*
  55.  
  56. Result from query above:
  57.  
  58. (0 row(s) affected)
  59. The following error occurred:
  60. Divide by zero error encountered.
  61. Msg 8134, Level 16, State 1, Line 6
  62. Divide by zero error encountered.
  63.  
  64. DETAIL OF THE RESULTS ABOVE
  65. (0 row(s) affected)
  66. -- error message from the CATCH block, seen and handled by the user but not the client application
  67. The following error occurred:
  68. Divide by zero error encountered.
  69. -- error message from the THROW, seen and handled by the client application (SQL Server)
  70. Msg 8134, Level 16, State 1, Line 6
  71. Divide by zero error encountered.
  72.  
  73. */
  74.  
  75. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  76. -- example of more complicated version that CATCHes error and writes it to the AdventureWorks dbo.ErrorLog table, using the pre-existing dbo.uspLogError stored procedure. --
  77. -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  78.  
  79. -- begins TRY block
  80. BEGIN TRY
  81. UPDATE Production.Product2
  82. -- statement that will intentionally cause a divide by zero error
  83. SET StandardCost = ProductID / 0;
  84. END TRY
  85. -- begins CATCH block which will execute if there's an error in TRY block
  86. BEGIN CATCH
  87. -- delcares variables @ErrorLogId to hold ID, @ErrorMsg to hold message
  88. DECLARE @ErrorLogID AS INT, @ErrorMsg AS VARCHAR(250);
  89. -- pre-existing dbo.uspLogError stored procedure that logs the error into pre-existing table dbo.ErrorLog with @ErrorLogID as an OUTPUT parameter
  90. EXECUTE dbo.uspLogError @ErrorLogID OUTPUT;
  91. -- sets error message to this text, inserting @ErrorLogID so the user can see which ErrorLogID to look at in dbo.ErrorLog for more information
  92. SET @ErrorMsg = 'The update failed because of an error. View error #'
  93. + CAST(@ErrorLogID AS VARCHAR)
  94. + ' in the error log for details.';
  95. -- THROWs custom error showing user contents of @ErrorMsg variable
  96. THROW 50001, @ErrorMsg, 0;
  97. END CATCH;
Add Comment
Please, Sign In to add comment