SQL Server, how does Engine manage errors behind the scenes?

Hernan C 1 Reputation point
2021-12-03T19:13:25.437+00:00

I have a SQLServer Stored Procedure,,that is called by a client application (Java/Hibernate/JDBC)

I catch errors with TRY/CATCH or @@error variable. That´s fine

But, what does the Engine do in the case of an error ?

It seems that in some way, every error is reported to client application
even though you have caught it

Then, the client application triggers an Exception
because of errors that have already been treated -- in SQL Server
and shouldn´t have been sent to the client

Does Engine always report to client all errors?
It depends on the driver?

Is it possible to stop Engine to send caught/treated messages ?

I didn´t found any explanation on internet....

I´ll appreciate your help

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,713 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Tom Phillips 17,716 Reputation points
    2021-12-03T19:48:00.21+00:00

    try/catch/@@error cannot catch all errors. Most likely your error is a severity >10, which terminates at the point of the error and does not continue the command.

    See:
    https://learn.microsoft.com/en-us/sql/t-sql/language-elements/try-catch-transact-sql?view=sql-server-ver15#remarks


  2. Erland Sommarskog 101K Reputation points MVP
    2021-12-03T22:47:34.217+00:00

    First of all, if there is an unexpected error in your SQL code, it should be trapped by TRY-CATCH, and then it should be re-raised by the CATCH block, so it should go to the client.

    Occasionally, you may perform an action, fully aware of that it can go wrong and for that case you have a plan B. In this case you do not want the error to go the client, but that is not a very common thing.

    You should be aware of that error handling in SQL Server is a very messy topic, and there are errors you cannot catch, or you cannot catch in the scope they occur.

    But if you don't see any exception in SSMS, you should not see any in the application. So we really need to have a more concrete example of what you are talking about.


  3. Dan Guzman 9,206 Reputation points
    2021-12-04T18:35:20.59+00:00

    Since the same proc works from SSMS, it may be the client code is using a server API cursor (which SSMS does not do) and the options are incompatible with the proc. That error cannot be caught on the server side. Without debugging the client code, you could run a server-side trace to troubleshoot.

    0 comments No comments

  4. Seeya Xi-MSFT 16,436 Reputation points
    2021-12-06T08:58:23.547+00:00

    Hi @Hernan C ,

    I suggest you read this article.
    https://www.mssqltips.com/sqlservertip/6478/error-handling-in-sql-server-with-try-catch/
    In addition, please post your try catch code, it will be very helpful to solve the problem.

    Best regards,
    Seeya


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments