So in Sql Server it appears that I cannot have specific steps within my catch branch like I can in PL/SQL (I could add IF / ELSE checks).
So in PL/SQL I would have something like this
DECLARE
MY_EXCEPTION EXCEPTION;
BEGIN
//My Error is Raised
EXCEPTION
WHEN MY_EXCEPTION THEN
//Perform actions
END
How are others handling this? Is there a more elegant solution with the TRY / CATCH than using IF Statements to look at the errors and perform operations?
Thanks,
S
What kind of operations? Do you mean you want to declare elsewhere that for exception A it gets logged and exception B just gets ignored, and have your catch block inherit those actions? SQL Server 2012 adds
THROWso that you can do other things (log, send e-mail, whatever) and then essentially re-throw the error that triggered the catch in the first place – but there is no way to define error handling as such centrally unless you pass the error number, severity etc. to a stored procedure (then the logic could be done in the procedure instead of in theCATCHblock). Quick example:Then you could play with various exceptions (well, ones that pass the parsing phase, at least):
For the bible on error handling see Erland’s articles http://www.sommarskog.se/error-handling-I.html, http://www.sommarskog.se/error-handling-II.html and http://www.sommarskog.se/error_handling_2005.html