Using Try..Catch in Natively Compiled Stored Procedures
You can use try..catch blocks inside a natively compiled stored procedure. The following constructs are supported:
ERROR_LINE
ERROR_MESSAGE
ERROR_NUMBER
ERROR_PROCEDURE
ERROR_SEVERITY
ERROR_STATE
CREATE PROCEDURE test_try_catch
with native_compilation, schemabinding, execute as owner
as
begin atomic with (transaction isolation level = snapshot, language = N'us_english')
BEGIN TRY
-- generate error
declare @i int = 1,
@j int = 0
select @i/@j
END TRY
BEGIN CATCH
-- Execute error retrieval routine.
SELECT
ERROR_SEVERITY() AS ErrorSeverity
,ERROR_STATE() AS ErrorState
,ERROR_PROCEDURE() AS ErrorProcedure
,ERROR_LINE() AS ErrorLine
,ERROR_MESSAGE() AS ErrorMessage
END CATCH
end
go
exec test_try_catch
go