SQL Stored Procedure Exception Not Found in JAVA

2019-09-10 00:48发布

问题:

Throwing exception from sql stored procedure catch block is not raising exception in java layer for SP1 while for SP2 exception raised.

When I am executing both SPs are giving exception in SQL Server but from java code I got no exception for SP1.

I am calling both SPs using JPA StoredProcedureQuery and using Hibernate & Spring.

CREATE PROCEDURE SP1
AS
SET NOCOUNT ON

BEGIN TRY
DECLARE @ErrorMessage AS VARCHAR(MAX)
SELECT 1/0
END TRY

BEGIN CATCH
THROW 50001, @ErrorMessage, 1;
END CATCH


CREATE PROCEDURE SP2
AS
SET NOCOUNT ON

DECLARE @ErrorMessage AS VARCHAR(MAX)
SELECT 1/0
if @@error<>0
GOTO Erro

RETURN
Erro:
THROW 50001, @ErrorMessage, 1;

I have tested that if I use any THROW in TRY block it raise exception in Java side while in CATCH block its not.