Implement Error Handling With T-SQL
Implement Error Handling With T-SQL
In this lab, you’ll use T-SQL statements to test various error handling techniques in
the adventureworks database. For your reference, the following diagram shows the tables in
the database (you may need to resize the pane to see them clearly).
Result
Conversion failed when converting the nvarchar value ‘Some text’ to data type int.
3. Write a TRY/CATCH construct. Your T-SQL code should look like this:
CodeCopy
BEGIN TRY
SELECT CAST(N'Some text' AS int);
END TRY
BEGIN CATCH
PRINT 'Error';
END CATCH;
1. Run the modified code, and review the response. The results should include
no rows, and the Messages tab should include the text Error.
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
END CATCH;
1. Select ⏵Run. Notice that you didn’t get an error because you used the
TRY/CATCH construct.
2. Modify the T-SQL code by adding two PRINT statements. The T-SQL code
should look like this:
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS varchar(10));
PRINT 'Error Message: ' + ERROR_MESSAGE();
END CATCH;
1. Run the modified code, and notice that an error is produced, but it’s one that
you defined.
2. Now change the value of the @num variable to look like this:
CodeCopy
1. Run the modified code. Notice that you get a different error number and
message.
1. Run the modified code. Notice that you get a different error number and
message.
Started executing query at line 1
Error Message: Arithmetic overflow error converting varchar to data type numeric.
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
IF ERROR_NUMBER() IN (245, 8114)
BEGIN
PRINT 'Handling conversion error...'
END
ELSE
BEGIN
PRINT 'Handling non-conversion error...';
END;
1. Run the modified code. You’ll see that message returned now contains more
information:
1. Run the modified code. This produces a different type of error message:
CodeCopy
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
EXECUTE dbo.GetErrorInfo;
END CATCH;
1. Run the code. This will trigger the stored procedure and display:
Started executing query at line 1
Error Severity: 16
Error State: 1
Error Line: 4
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
EXECUTE dbo.GetErrorInfo;
THROW;
END CATCH;
1. Run the modified code. Here you’ll see that it executes the stored procedure,
and then throws the error message again (so a client application can catch
and process it).
Error Severity: 16
Started executing query at line 1
Error State: 1
Error Line: 4
CodeCopy
BEGIN TRY
PRINT 5. / CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
EXECUTE dbo.GetErrorInfo;
IF ERROR_NUMBER() = 8134
BEGIN
PRINT 'Handling devision by zero...';
END
ELSE
BEGIN
PRINT 'Throwing original error';
THROW;
END;
END CATCH;
1. Run the modified code As you’ll see, it executes the stored procedure to
display the error, identifies that it isn’t error number 8134, and throws the
error again.
Error Severity: 16
Error State: 5
Error Line: 5
Challenges
Now it’s time to try using what you’ve learned.
Tip: Try to determine the appropriate solutions for yourself. If you get stuck, suggested answers are
provided at the end of this lab.
The marketing manager is using the following T-SQL query, but they are getting
unexpected results. They have asked you to make the code more resilient, to stop it
crashing and to not display duplicates when there is no data.
CodeCopy
Error messages and error handling are essential for good code. Your manager has
asked you to develop a common error display procedure. Use this sample code as
your base.
CodeCopy
Challenge Solutions
This section contains suggested solutions for the challenge queries.
Challenge 1
CodeCopy
END CATCH;
SET @maxReturns += 1;
SET @CustomerID += 1;
END;
CodeCopy
SET @maxReturns += 1;
SET @CustomerID += 1;
END;
CodeCopy
IF @@ROWCOUNT > 0
BEGIN
PRINT CAST(@customerID as NVARCHAR(20)) + N' ' + @fname + N' ' +
@lname;
END
END TRY
BEGIN CATCH
PRINT 'Unable to run query'
END CATCH
SET @maxReturns += 1;
SET @CustomerID += 1;
END;
Challenge 2
CodeCopy
BEGIN TRY
PRINT 'Casting: ' + CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
END CATCH;
CodeCopy
CodeCopy
BEGIN TRY
PRINT 'Casting: ' + CAST(@num AS numeric(10,4));
END TRY
BEGIN CATCH
EXECUTE dbo.DisplayErrorDetails;
END CATCH;