SQL Server ERROR_SEVERITY(): A How-To Guide
In this article
ERROR_SEVERITY() returns the severity level of the error that caused the CATCH block to run. You call it inside a TRY...CATCH block; outside a CATCH block it returns NULL, and the value is only meaningful while the CATCH block is executing. The severity is a small integer (0–25) that tells you how serious the error is, so you can decide whether to log it, retry, or abort the batch.
What You'll Need
- SQL Server 2005 or later (ERROR_SEVERITY was introduced with TRY...CATCH in SQL Server 2005).
- A database where you can create a test table or run a simple query.
- Permission to run the query and, if you will insert into a log table, INSERT permission on that table.
- SQL Server Management Studio (SSMS) or another client that can execute T-SQL.
Step-by-Step
- Open a query window in SSMS and connect to the database you want to test against. The function works in any database context, but your test objects should live somewhere you can clean up.
- Write a basic TRY...CATCH block. The pattern is:
BEGIN TRY
SELECT 1/0;
END TRY
BEGIN CATCH
SELECT ERROR_SEVERITY() AS Severity;
END CATCH;
Run it. You should get a severity of 16, which is the typical severity for a statement-level error such as divide-by-zero. - Add the other error functions for context. ERROR_SEVERITY() is most useful alongside ERROR_NUMBER(), ERROR_MESSAGE(), ERROR_LINE(), ERROR_PROCEDURE(), and ERROR_STATE(). Select them together so you can see the full picture:
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS Severity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure, ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage; - Test with a user-raised error. Use RAISERROR or THROW to produce a specific severity. For example,
RAISERROR('Test error', 16, 1);inside the TRY block will be caught, and ERROR_SEVERITY() will return 16. THROW with a severity of 16 behaves similarly. Severities 11–16 are user-correctable errors; 17–19 are resource or configuration problems; 20–25 are fatal and terminate the connection, so they are not catchable in the same way. - Log the severity. Create a simple log table if you do not have one:
CREATE TABLE dbo.ErrorLog (LogID INT IDENTITY PRIMARY KEY, ErrorTime DATETIME2 DEFAULT SYSDATETIME(), ErrorNumber INT, Severity INT, ErrorState INT, ErrorProcedure NVARCHAR(128), ErrorLine INT, ErrorMessage NVARCHAR(4000));
Then in the CATCH block:INSERT dbo.ErrorLog (ErrorNumber, Severity, ErrorState, ErrorProcedure, ErrorLine, ErrorMessage)
SELECT ERROR_NUMBER(), ERROR_SEVERITY(), ERROR_STATE(), ERROR_PROCEDURE(), ERROR_LINE(), ERROR_MESSAGE(); - Use the severity to branch logic. A common pattern is to re-raise low-severity errors and abort on high ones. For example, if ERROR_SEVERITY() is 16, you might log and return a friendly message; if it is 20 or higher, you might let the error propagate. Remember that a CATCH block does not automatically stop the batch; you must use THROW or RAISERROR to re-raise.
- Check the value outside a CATCH block. Run
SELECT ERROR_SEVERITY();on its own. It returns NULL, which confirms the function is scoped to the CATCH block. This is a quick way to verify you are in the right context.
Common Mistakes
- Calling it outside a CATCH block. The function returns NULL, and any logic that compares it to a number will silently fail or behave unexpectedly.
- Assuming severity is always 16. Many application errors are 16, but constraint violations, conversion errors, and permission errors can carry different severities. Always read the value rather than hard-coding it.
- Expecting to catch fatal errors. Severity 20–25 errors terminate the connection and are not caught by TRY...CATCH. Do not rely on ERROR_SEVERITY() to handle those.
- Forgetting that the CATCH block runs in a different scope. Variables declared in the TRY block may not be available or may have default values in the CATCH block. Use the ERROR_ functions instead of relying on local variables.
- Not re-raising. If you swallow the error and only log it, the caller may think the operation succeeded. Decide explicitly whether to THROW.
Tips
- Store severity as a small integer column; it fits in TINYINT but INT is simpler and avoids conversion surprises.
- Combine ERROR_SEVERITY() with ERROR_NUMBER() in your alerting rules. Severity alone is a coarse filter; the number identifies the specific error.
- In nested TRY...CATCH blocks, the ERROR_ functions return information about the error that caused the current CATCH block to run, not the outermost error. If you re-raise, the outer CATCH sees the re-raised error.
- Use THROW rather than RAISERROR in new code; THROW preserves the original error number and severity when re-raising without parameters.
- Test your CATCH logic with deliberate errors in a development database before relying on it in production.
Troubleshooting
If ERROR_SEVERITY() returns NULL, confirm the call is inside a CATCH block and that the CATCH block is actually executing. A common cause is that the error occurred outside the TRY block, or the TRY block completed without error. Add a PRINT or SELECT at the top of the CATCH block to verify it runs.
If the severity value seems wrong, check whether the error was re-raised. A re-raised error can change the severity. Also confirm you are not reading a stale value from a previous error; the ERROR_ functions are only valid during the CATCH block and are reset afterward.
If the CATCH block does not run at all for a high-severity error, that is expected for severity 20 and above. Those errors terminate the connection and cannot be caught. For severity 17–19, some errors are catchable and some are not; test the specific error you care about.
If you get a syntax error near ERROR_SEVERITY, check that you are using the function with parentheses and that you are on SQL Server 2005 or later. The function is not available in older versions.
Frequently Asked Questions
What does ERROR_SEVERITY() return outside a CATCH block?
It returns NULL. The function is only meaningful while a CATCH block is executing.
Can I use ERROR_SEVERITY() in a stored procedure?
Yes, inside a TRY...CATCH block within the procedure. The value reflects the error that caused the CATCH block to run.
What is the difference between ERROR_SEVERITY() and ERROR_STATE()?
Severity indicates how serious the error is (0–25). State is a numeric value used by the source of the error to identify a specific condition; it is not a severity measure.
Does ERROR_SEVERITY() work with THROW?
Yes. When a THROW statement raises an error that is caught, ERROR_SEVERITY() returns the severity specified in the THROW (or the original severity when re-throwing without parameters).
Why do I see severity 16 for most errors?
Severity 16 is the default for many user-correctable errors, including divide-by-zero and some constraint violations. Other errors can have different severities, so always read the value.
Can I catch severity 20 errors with TRY...CATCH?
No. Severity 20–25 errors are fatal and terminate the connection, so they are not caught by TRY...CATCH.