> For the complete documentation index, see [llms.txt](https://dailyjournal.gitbook.io/notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dailyjournal.gitbook.io/notes/databases/relational-databases/microsoft-sql-server/error-handling.md).

# Error Handling

|                                   System Errors                                   |                                       Custom Errors                                       |
| :-------------------------------------------------------------------------------: | :---------------------------------------------------------------------------------------: |
| it can be viewed in the `sys.messages` system-view and are defined by SQL Server. | to add a custom error message to the`sys.messages`stored procedure`sp_addmessage`is used. |
|                                                                                   |       they are generated by T-SQL custom code based on your code or business logic.       |

```sql
EXEC sp_addmessage message_id, severity,
N'error_message'
GO;
```

> **message\_id** is of `integer` type and ranges from 50,001 to 2,147,483,647.\
> **severity** is of `smallint` type and ranges from 1 through 25.

### Raising Error with `RAISERROR`

`RAISERROR` allows SQL server to generate an error that could be caught by the calling process.

```sql
RAISERROR ( { msg_id | msg_str | @local_variable }  
    { ,severity ,state }  
    [ ,argument [ ,...n ] ] )  
    [ WITH option [ ,...n ] ]
```

### Raising Error with `THROW`

```sql
THROW [ { error_number | @local_variable },  
        { message | @local_variable },  
        { state | @local_variable } ]   
[ ; ]
```

### `@@ERROR`

`@@ERROR` is a system variable that holds the error number of the last error occured.

* Disadvantage
  * the value that @@ERROR holds resets as a new error gets generated.

```sql
SELECT @@ERROR;
```

When working with `@@ERROR`, it is recommended to capture the error number in separate a variable as soon as error occurs.

```sql
DECLARE @error_value int;

RAISERROR(N'message', 16, 1);
SET @error_value = @@ERROR;

IF @error_value <> 0
    PRINT 'Error=' + CAST(@error_value AS VARCHAR(8));
```

{% code title="Output" %}

```sql
Msg 50000, Level 16, State 1, Line 2
Message
Error=50000
```

{% endcode %}

### TRY...CATCH

```sql
BEGIN TRY
     { sql_statement | statement_block }
END TRY
BEGIN CATCH
     { sql_statement | statement_block }
END CATCH
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://dailyjournal.gitbook.io/notes/databases/relational-databases/microsoft-sql-server/error-handling.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
