You can then get the error number to take action. Here is an example of finding out the number: To find out what error number was produced by your code, you can inquire about the value produced by the ERROR_NUMBER() function. To do that, you can write an IF conditional statement. If/once you know the error number, you can take an appropriate action. At the least you can display a message. Here is an example: Of course, you can take better action than that. In previous versions of Microsoft SQL Server, the means of getting an error number was to call a function named @@ERROR. You can still use this function to find out what the error number is in order to take an appropriate action. Its syntax is: int @@ERROR(); This function can be called to get the error number produced by an exception. Here is an example; USE Exercise; GO BEGIN TRY DECLARE @Number tinyint, @Result tinyint; SET @Number = 252; SET @Result = @Number + 20; SELECT @Number AS Number, @Result AS Result; END TRY BEGIN CATCH PRINT N'Error #: ' + CAST(@@ERROR AS NVARCHAR(50)); END CATCH This would produce: Just as done for the ERROR_NUMBER() function, you can check the value of the @@ERROR call to find out what the error number is, and if it is the right number you are looking for, you can take appropriate action.
An error number is just a number built in the function code and known by the database engine. That number does not give any meaningful indication about the error. To give you a message related to the error, Transact-SQL provides the ERROR_MESSAGE() function. Its syntax is: nvarchar ERROR_MESSAGE(); This function takes no argument and it returns a string. Here is an example of calling it: USE Exercise; GO BEGIN TRY DECLARE @Number tinyint, @Result tinyint; SET @Number = 252; SET @Result = @Number + 20; SELECT @Number AS Number, @Result AS Result; END TRY BEGIN CATCH PRINT ERROR_MESSAGE(); END CATCH This would produce: Because you are a programmer, you should understand the meaning of the words overflow, error, and tinyint. Unfortunately, this message may not be very clear to a regular user. For this reason, you should provide an easy way to read the message. You can even combine your own error message to the value of the ERROR_MESSAGE() function.
Errors have different levels of consideration. Some must be dealt with as soon as possible while others can wait. To help you identify the severity of an error, Transact-SQL provides the ERROR_SEVERITY() function. Its syntax is: int ERROR_SEVERITY(); This function takes no argument and returns an integer. Here is an example of calling it to identify the severity of an error: USE Exercise; GO BEGIN TRY DECLARE @Number tinyint, @Result tinyint; SET @Number = 252; SET @Result = @Number + 20; SELECT @Number AS Number, @Result AS Result; END TRY BEGIN CATCH PRINT N'Error Severity: ' + CAST(ERROR_SEVERITY() AS nvarchar(100)); END CATCH This would produce:
The value of this number is not a level of severity. It is just an indication of the severity. You as the database developer must find out what this number is and take appropriate action. You can write an IF conditional statement to find out the value produced by this function and do what you judge necessary.
The state of an error is a number that specifies the section of code where an error occurred. This is because the same code can produce different errors at different sections of the code. To help you identify the state of an error, Transact-SQL provides the ERROR_STATE() function. Its syntax is: int ERROR_STATE(); This function takes no argument. It returns an integer that specifies the state of the error. This function is used with the same approach as the severity of an error.
Just as you can write various statements in your code, you can also create various exception sections. Here are examples: USE Exercise; GO BEGIN TRY DECLARE @Number tinyint, @Result tinyint; SET @Number = 252; SET @Result = @Number + 2; PRINT N'Number = ' + CAST(@Number AS nvarchar(20)); PRINT N'Result = ' + CAST(@Result AS nvarchar(20)); END TRY BEGIN CATCH PRINT N'Error: ' + ERROR_MESSAGE(); END CATCH PRINT N'---------------------------------------'; BEGIN TRY DECLARE @Value decimal(6,2), @Division decimal; SET @Value = 15.50; SET @Division = @Value / 2; PRINT N'Number = ' + CAST(@Value AS nvarchar(20)); PRINT N'Result = ' + CAST(@Division AS nvarchar(20)); END TRY BEGIN CATCH PRINT N'Error: ' + ERROR_MESSAGE(); END CATCH This would produce: In this case, each section would handle its own exception.
You can create an exception handling code inside of another. This is referred to as nesting. The basic formula is: BEGIN TRY BEGIN TRY -- Nested try block END TRY BEGIN CATCH -- Nested catch block END CATCH END TRY BEGIN CATCH END CATCH
If an error occurs in your code, you can take initiative for it, as we have done so far. To better customize how an exception is handled when it occurs, you can raise an error. To support this, Transact-SQL provides the RAISERROR() function. Its syntax is: RAISERROR ( { msg_id | msg_str | @local_variable } { ,severity ,state } [ ,argument [ ,...n ] ] ) [ WITH option [ ,...n ] ] This function takes three required arguments:
|
|
|||||||||||||||||||||||||||||
|