You are on page 1of 4

Error Handling

Error handling refers to the routines in a program that respond to abnormal input or conditions. The
quality of such routines is based on the clarity of the error messages and the options given to users for
resolving the problem.

Example
INSERT INTO CUSTOMER
values
(1,1,'EMEBET','WORKU','NEGUSSE',2,212,918619704,
912661132, 'emebetworku211912@gmail.com')

Using @@ERROR function


DECLARE @Error INT;
INSERT INTO CUSTOMER
values
(1,1,'EMEBET','WORKU','NEGUSSE',2,212,918619704,
912661132, 'emebetworku211912@gmail.com')
SET @Error = @@ERROR;
IF @Error > 0 BEGIN
PRINT @Error;
END;

@@ERROR function
Returns a non zero integer error number if the previous Transact-SQL statement encountered an error. It
returns 0 if the previous Transact-SQL statement encountered no errors.
If the error was one of the errors in the sys.messages catalog(Contains both system-defined and user-
defined messages in the system) view, then @@ERROR contains the value from the
sys.messages.message_id column for that error. And the text associated with an @@ERROR error
number is displayed in sys.messages
Note :Because @@ERROR is cleared and reset on each statement executed, a statement which checks its
value must follow the statement being verified immediately or it should be saved to a local variable that
can be checked later.

Database error 2627: Violation of PRIMARY KEY constraint 'PK__DialogPr__2B66FD76B6C705A1'.


Cannot insert duplicate key in object dbo.DialogProcess'."

Using TRY...CATCH construct to handle errors.

BEGIN TRY
{ sql_statement | statement_block }
END TRY
BEGIN CATCH
[ { sql_statement | statement_block } ]
END CATCH

[ ; ]
If there are no errors in the code that is enclosed in a TRY block, when the last statement in the TRY
block has finished running, control passes to the statement immediately after the associated END CATCH
statement.
If there is an error in the code that is enclosed in a TRY block, control passes to the first statement in the
associated CATCH block. When the code in the CATCH block finishes, control passes to the statement
immediately after the END CATCH statement.
The TRY...CATCH construct also supports additional system functions (ERROR_LINE,
ERROR_MESSAGE, ERROR_PROCEDURE, ERROR_SEVERITY, and ERROR_STATE) that return
more error information than @@ERROR.
Note Errors trapped by a CATCH block are not returned to the calling application. If any part of the error
information must be returned to the application, the code in the CATCH block must do so by using
mechanisms such as SELECT result sets or the RAISERROR and PRINT statements.
error number in the statement immediately after the statement that generated an error.

BEGIN TRY
INSERT INTO CUSTOMER
values
(1,1,'EMEBET','WORKU','NEGUSSE',2,212,918619704,
912661132, 'emebetworku211912@gmail.com')
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber,ERROR_MESSAGE() AS ErrorMessage,
ERROR_SEVERITY() AS ErrorSeverity;

END CATCH;

When called in a CATCH block, ERROR_NUMBER returns the error number of the error that caused the
CATCH block to run.

ERROR_NUMBER returns NULL when called outside the scope of a CATCH block.

You might also like