# Raise
{: .no_toc }

Generates a run-time error.

Syntax: **Err**.**Raise** *number* [ **,** *source* [ **,** *description* [ **,** *helpfile* [ **,** *helpcontext* ] ] ] ]

*number*
: *required* A **Long** that identifies the nature of the error. Built-in errors fall in the range 0--65535; the range 0--512 is reserved for system errors and 513--65535 is available for user-defined errors. When raising a user-defined error from a class module, add the chosen number to the [**vbObjectError**](/tB/Modules/Constants/index.md#vbObjectError) constant --- for example, `vbObjectError + 513`.

*source*
: *optional* A **String** naming the object or application that generated the error. When setting the [**Source**](/tB/Modules/ErrObject/Source.md) property for an object, use the form *project.class*. If *source* is not specified, [**Source**](/tB/Modules/ErrObject/Source.md) is an empty string.

*description*
: *optional* A **String** describing the error. If unspecified, [**Description**](/tB/Modules/ErrObject/Description.md) is chosen from *number*, as the table below shows.

*helpfile*
: *optional* The fully qualified path to the Help file in which help on this error can be found. If unspecified, the [**HelpFile**](/tB/Modules/ErrObject/HelpFile.md) property is cleared.

*helpcontext*
: *optional* The context ID identifying a topic within *helpfile* that provides help for the error. If omitted, the [**HelpContext**](/tB/Modules/ErrObject/HelpContext.md) property is cleared.

All of the arguments are optional except *number*. An omitted argument is never taken from an earlier error: each call to **Raise** sets all five properties of the **Err** object, and the ones it is not given are empty or 0.

When *description* is omitted, **Description** depends on *number*:

| *number* | **Description** |
|----------|-----------------|
| has a built-in run-time error message, such as 5 or 11 | the text that the [**Error**](/tB/Modules/Conversion/Error.md) function returns for it, such as `Invalid procedure call or argument` |
| from 1 to 746 and has no built-in message, such as 1, 95, 99 or 513 | an empty string |
| 747 or more | the Windows system message for that number when there is one, as for 1001 (`Recursion too deep; the stack overflowed.`), and otherwise `Automation error` |
| negative | the Windows system message for that value as an **HRESULT** when there is one, and otherwise `Automation error` |

A *description* of an empty string gives an empty **Description** for every number. The system messages are in the language of the system and differ between Windows versions; `Automation error` is the only text of the table that does not come from the system.

> [!NOTE]
> **Raise** accepts a *number* above 65535 and raises that number. VB6 and 64-bit VBA raise error 5, *Invalid procedure call or argument*, for one.

When the error is raised in a method of a class and the caller handles it, an empty **Description** reaches the caller as `Application-defined or object-defined error`. The other texts arrive unchanged.

> [!WARNING] BETA 1045
> **Raise** differs from VBA and VB6 in three ways, and none of them raises an error. Without *source*, VBA sets **Source** to the name of the project, and twinBASIC leaves it empty. Without *description*, VBA gives every number that has no built-in message the text `Application-defined or object-defined error`, from 1 up to 65535, and twinBASIC gives an empty string or one of the texts above. And an omitted argument keeps the value left by an earlier error in VBA, and is reset in twinBASIC. A program that relies on **Err.Source** being the project name, or on **Err.Description** being non-empty, has to pass those arguments.

> [!NOTE] BETA 1045
> An error that a class member raises with a constant *number*, such as `Err.Raise 380`, reaches a caller that called the member late-bound --- through an **Object** variable or [**CallByName**](/tB/Modules/Interaction/CallByName.md) --- as error -2147352567 (`&H80020009`), *Exception occurred.*, not as 380. VB6 raises 380. The number arrives unchanged when the member passes it in a variable, as in `Err.Raise Number`, and in every early-bound call.

> [!NOTE] BETA 1045
> **Raise** raises an **HRESULT** *number* as it is. VB6 and 64-bit VBA raise many **HRESULT**s as the VB error of the same meaning instead, including these:
>
> | *number* | VB6 and 64-bit VBA raise |
> |---|---|
> | `&H80070057` (`E_INVALIDARG`) | 5 |
> | `&H8007000E` (`E_OUTOFMEMORY`) | 7 |
> | `&H80070005` (`E_ACCESSDENIED`), `&H80030005` (`STG_E_ACCESSDENIED`) | 70 |
> | `&H80004001` (`E_NOTIMPL`) | 445 |
> | `&H80004002` (`E_NOINTERFACE`) | 430 |
> | `&H80004004` (`E_ABORT`) | 287 |
> | `&H80020003` (`DISP_E_MEMBERNOTFOUND`), `&H80020006` (`DISP_E_UNKNOWNNAME`) | 438 |
> | `&H80020005` (`DISP_E_TYPEMISMATCH`) | 13 |
> | `&H80020008` (`DISP_E_BADVARTYPE`) | 458 |
> | `&H8002000A` (`DISP_E_OVERFLOW`) | 6 |
> | `&H8002000B` (`DISP_E_BADINDEX`) | 9 |
> | `&H8002000E` (`DISP_E_BADPARAMCOUNT`) | 450 |
> | `&H80020011` (`DISP_E_NOTACOLLECTION`) | 451 |
> | `&H80030002` (`STG_E_FILENOTFOUND`) | 432 |
> | `&H80030003` (`STG_E_PATHNOTFOUND`) | 76 |
> | `&H80030070` (`STG_E_MEDIUMFULL`) | 61 |
> | `&H80040154` (`REGDB_E_CLASSNOTREG`), `&H80040112` (`CLASS_E_NOTLICENSED`), `&H800401E3` (`MK_E_UNAVAILABLE`), `&H800401F3` (`CO_E_CLASSSTRING`) | 429 |
> | `&H800A0001` to `&H800AFFFF`, a VB error as an **HRESULT** | its low 16 bits: 5 for `&H800A0005`, 53 for `&H800A0035` |
>
> They keep others as they are, such as `&H80004005` (`E_FAIL`), `&H80020009` (`DISP_E_EXCEPTION`) and `&H80070002`. A handler that tests for a VB number, such as 5, does not recognise the **HRESULT** that twinBASIC raises.

**Raise** is preferred over the [**Error**](/tB/Core/Error.md) statement when generating run-time errors, particularly inside class modules: the **Err** object holds richer information than the **Error** statement can supply. With **Raise** the source that generated the error can be specified in the [**Source**](/tB/Modules/ErrObject/Source.md) property, online Help for the error can be referenced through [**HelpFile**](/tB/Modules/ErrObject/HelpFile.md) and [**HelpContext**](/tB/Modules/ErrObject/HelpContext.md), and so on.

### Common Errors

| Error | When |
|---|---|
| [5 (`&H5`)](/Reference/Run-time-Errors.md#5), *Invalid procedure call or argument* | *number* is 0 after rounding, or **Empty** |
| [94 (`&H5E`)](/Reference/Run-time-Errors.md#94), *Invalid use of Null* | an argument is **Null** |

### Example

This example uses the **Raise** method of the **Err** object to generate an error from inside an Automation object with the programmatic ID `MyProj.MyObject`.

```tb check_build
Const MyContextID As Long = 1010407    ' Define a constant for the contextID.

Function TestName(ByVal CurrentName As String, ByVal NewName As String)
    If InStr(NewName, "bob") Then    ' Test the validity of NewName.
        Err.Raise vbObjectError + 513, "MyProj.MyObject", _
                  "No ""bob"" allowed in your name", _
                  "C:\MyProj\MyHelp.chm", MyContextID
    End If
End Function
```

This example raises errors without a source or a description, and prints what **Err** holds for each:

```tb check_run id=err-raise-texts
On Error Resume Next
Err.Raise 5
Debug.Print "[" & Err.Source & "] [" & Err.Description & "]"   ' [] [Invalid procedure call or argument]
Err.Raise 513
Debug.Print "[" & Err.Source & "] [" & Err.Description & "]"   ' [] []
Err.Raise 1000
Debug.Print "[" & Err.Source & "] [" & Err.Description & "]"   ' [] [Automation error]
Err.Raise 1000, "MyProj.MyObject", "Out of paper"
Debug.Print "[" & Err.Source & "] [" & Err.Description & "]"   ' [MyProj.MyObject] [Out of paper]
```

### See Also

- [Number](/tB/Modules/ErrObject/Number.md) property
- [Source](/tB/Modules/ErrObject/Source.md) property
- [Description](/tB/Modules/ErrObject/Description.md) property
- [HelpFile](/tB/Modules/ErrObject/HelpFile.md) property
- [HelpContext](/tB/Modules/ErrObject/HelpContext.md) property
- [Clear](/tB/Modules/ErrObject/Clear.md) method
- [Error](/tB/Core/Error.md) statement
