Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:00:50+00:00 2026-05-30T03:00:50+00:00

So in Sql Server it appears that I cannot have specific steps within my

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:00:51+00:00Added an answer on May 30, 2026 at 3:00 am

    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 THROW so 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 the CATCH block). Quick example:

    CREATE PROCEDURE dbo.CustomErrorHandler
        @ErrorNumber INT,
        @ProcID INT
    AS
    BEGIN
        IF @ErrorNumber = 8134
        BEGIN
            PRINT 'Oh, it was just divide by 0 in '
                + COALESCE(OBJECT_NAME(@ProcID), 'ad hoc');
            RETURN;
        END
    
        IF @ErrorNumber = 208
        BEGIN
            PRINT 'Invalid object access!';
            -- send e-mail about invalid object access
            RETURN;
        END
    
        /* other custom handling for other exceptions */
    
        RAISERROR('Unhandled exception I guess?', 11, 1);
    END
    GO
    

    Then you could play with various exceptions (well, ones that pass the parsing phase, at least):

    BEGIN TRY
        SELECT 1/0; --8134
        --EXEC('SELECT * FROM splunge;'); --208
    END TRY
    BEGIN CATCH
        DECLARE 
            @e INT = ERROR_NUMBER(),
            @p INT = @@PROCID;
    
        EXEC dbo.CustomErrorHandler @e, @p;
    END CATCH
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using SQL Server 2005: I have one stored proc that calls several others within
I have a large (100+ tables) SQL Server 2005 database that I would like
In SQL Server 2005 Management Studio, it appears that the output of PRINT statements
It appear that SQL Server like most other products Random Function really is not
Note: This issue appears to be limited to SQL Server 2005 SP2 I have
I have recently been running into many different areas of SQL Server that I
We have developed a .NET web application that uses SQL Server as a backend.
We have customers who, for unassailable reasons, cannot use SQL Server's built-in backup features
I have a SQL Server 2005 database that I'm trying to access as a
I ran into something that seems odd. SQL Server appears to be rounding some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.