Exception SQL Server

@@ERROR

Returns the error number for the last Transact-SQL statement excecuted

Systax

@@ERROR

Return Types

Integer

Remark

Returns 0 if the previous Transact-SQL statement encountered no errors

Exception

Implements error handling for Transact-SQL. A group of Transact-SQL statements can be enclosed in a TRY block. If an error occurs in the TRY block, control is passed to another group of statements that is enclose in a CATCH block.

Systax
BEGIN TRY
	{sql_statement | statement_block}
END TRY
BEGIN CATCH
	[{sql_statment | statement_block }]
END CATCH[;]

Exception

Example
BEGIN TRY
SELECT 1 / 0
END TRY
BEGIN CATCH
	SELECT ERROR_NUMBER(), ERROR_MESSAGE()
END CATCH

RAISERROR

Generates an error message and initiates error processing for the session. The message is returned as a server error message to the calling application or to an associted CATCH block of a TRY ... CATCH construct.

Syntax
RAISERROR ({msg_id | msg_str | @local_variable}, {,severity,state})

THROW