1BEGIN TRY
2 SELECT 1 / 0 AS Error;
3END TRY
4BEGIN CATCH
5 SELECT
6 ERROR_NUMBER() AS ErrorNumber,
7 ERROR_STATE() AS ErrorState,
8 ERROR_SEVERITY() AS ErrorSeverity,
9 ERROR_PROCEDURE() AS ErrorProcedure,
10 ERROR_LINE() AS ErrorLine,
11 ERROR_MESSAGE() AS ErrorMessage;
12END CATCH;
1
2// errror handeling in sql server
3
4 CREATE PROC usp_divide(
5 @a decimal,
6 @b decimal,
7 @c decimal output
8) AS
9BEGIN
10 BEGIN TRY
11 SET @c = @a / @b;
12 END TRY
13 BEGIN CATCH
14 SELECT
15 ERROR_NUMBER() AS ErrorNumber
16 ,ERROR_SEVERITY() AS ErrorSeverity
17 ,ERROR_STATE() AS ErrorState
18 ,ERROR_PROCEDURE() AS ErrorProcedure
19 ,ERROR_LINE() AS ErrorLine
20 ,ERROR_MESSAGE() AS ErrorMessage;
21 END CATCH
22END;
23GO
24
1BEGIN TRY
2 { sql_statement | statement_block }
3END TRY
4BEGIN CATCH
5 [ { sql_statement | statement_block } ]
6END CATCH
7[ ; ]
1
2
3
4
5 CREATE PROC usp_divide(
6 @a decimal,
7 @b decimal,
8 @c decimal output
9) AS
10BEGIN
11 BEGIN TRY
12 SET @c = @a / @b;
13 END TRY
14 BEGIN CATCH
15 SELECT
16 ERROR_NUMBER() AS ErrorNumber
17 ,ERROR_SEVERITY() AS ErrorSeverity
18 ,ERROR_STATE() AS ErrorState
19 ,ERROR_PROCEDURE() AS ErrorProcedure
20 ,ERROR_LINE() AS ErrorLine
21 ,ERROR_MESSAGE() AS ErrorMessage;
22 END CATCH
23END;
24GO
25