---
title: "SQL Server Exception Handling"  
description: "SQL Server Exception Handling"  
author: "Anonymous User"  
published: 2015-12-08  
updated: 2015-12-08  
canonical: https://www.mindstick.com/forum/33700/sql-server-exception-handling  
category: "database"  
tags: ["sql server", "sql", "sql server 2008", "sql server 2012"]  
reading_time: 1 minute  

---

# SQL Server Exception Handling

I want to use [Exception Handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling) in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) please help me.

## Replies

### Reply by Anonymous User

QL [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) also has an [exception](https://www.mindstick.com/articles/1824/objective-c-exception-handling) model to handle exceptions and errors that occurs in T-[SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) statements. To handle exception in Sql Server we have TRY..CATCH blocks. We put T-SQL statements in TRY block and to handle exception we write code in CATCH block. If there is an error in code within TRY block then the control will automatically jump to the corresponding CATCH blocks.The TRY...CATCH statement which helps us to handle the errors effectively in the back end. This [handling](https://www.mindstick.com/forum/34585/file-handling) of the exception can provide additional information about the errors.

```
BEGIN TRY  DECLARE @val INT  set @val=100/0 END TRY  BEGIN CATCH   SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_SEVERITY() AS ErrorSeverity, ERROR_STATE() AS ErrorState, ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine, ERROR_MESSAGE() AS ErrorMessage  END CATCH  GO
```

![SQL Server Exception Handling](https://www.mindstick.com/mindstickforums/1e369a87-26b2-4d54-9147-1f7f442d0715/images/af774ec9-fe28-46fe-b73e-e30cd6253fcc.png)


---

Original Source: https://www.mindstick.com/forum/33700/sql-server-exception-handling

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
