简体   繁体   中英

Raiserror in procedure (SQL Server 2014) does not catch client side(c#)

Stored procedure Catch block code.

alter PROCEDURE [dbo].[TESTError]      
(

)
AS
BEGIN

    SET NOCOUNT ON;

      BEGIN TRY
        SELECT 5/0
      END TRY 
    BEGIN CATCH

        DECLARE @ErrorNumber INT

           SELECT @ErrorNumber = ERROR_NUMBER() 

           RAISERROR
        (N'The error code is: %d',
             16, -- Severity.
              1, -- State.
             @ErrorNumber,     
             '');
END CATCH
END

The above stored procedure throw and show error when runs using SSMS.

.Net Client code only exception portion.

    catch (SqlException ex)
            {      
                string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
            }

When application called stored procedure it does not catch in exception block.

Any kind of hint or idea would help me.

catch (SqlException ex)
        {      
            string msg = string.Format("Error number: {0} / Message: {1}", ex.Number, ex.Message);
        }

What are you expecting to happen, even if this catch-block was entered? A string variable would get a value which is gone the next micro second...

Anyway: Whenever you deal with specialised (typed) catch blocks, you should have a good reason not to have a general catch-block additionally (as last of your specialised ones):

catch (System.Exception sysex)
        {      
            string msg = string.Format("Error number: {0} / Message: {1}", sysex.Number, sysex.Message);
            //Do something with this information!
        }

Or just

catch{throw;}

This depends on your needs and tools / habits of debugging...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM