1# Error
2
3Class `Napi::Error` inherits from class [`Napi::ObjectReference`][] and class [`std::exception`][].
4
5The `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown
6when runtime errors occur. The Error object can also be used as a base object for
7user-defined exceptions.
8
9The `Napi::Error` class is a persistent reference to a JavaScript error object thus
10inherits its behavior from the `Napi::ObjectReference` class (for more info see: [`Napi::ObjectReference`](object_reference.md)).
11
12If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
13`Napi::Error` class extends `std::exception` and enables integrated
14error-handling for C++ exceptions and JavaScript exceptions.
15
16For more details about error handling refer to the section titled [Error handling](error_handling.md).
17
18## Methods
19
20### New
21
22Creates empty instance of an `Napi::Error` object for the specified environment.
23
24```cpp
25Napi::Error::New(Napi::Env env);
26```
27
28- `[in] env`: The environment in which to construct the `Napi::Error` object.
29
30Returns an instance of `Napi::Error` object.
31
32### New
33
34Creates instance of an `Napi::Error` object.
35
36```cpp
37Napi::Error::New(Napi::Env env, const char* message);
38```
39
40- `[in] env`: The environment in which to construct the `Napi::Error` object.
41- `[in] message`: Null-terminated string to be used as the message for the `Napi::Error`.
42
43Returns instance of an `Napi::Error` object.
44
45### New
46
47Creates instance of an `Napi::Error` object
48
49```cpp
50Napi::Error::New(Napi::Env env, const std::string& message);
51```
52
53- `[in] env`: The environment in which to construct the `Napi::Error` object.
54- `[in] message`: Reference string to be used as the message for the `Napi::Error`.
55
56Returns instance of an `Napi::Error` object.
57
58### Fatal
59
60In case of an unrecoverable error in a native module, a fatal error can be thrown
61to immediately terminate the process.
62
63```cpp
64static NAPI_NO_RETURN void Napi::Error::Fatal(const char* location, const char* message);
65```
66
67The function call does not return, the process will be terminated.
68
69### Constructor
70
71Creates empty instance of an `Napi::Error`.
72
73```cpp
74Napi::Error::Error();
75```
76
77Returns an instance of `Napi::Error` object.
78
79### Constructor
80
81Initializes an `Napi::Error` instance from an existing JavaScript error object.
82
83```cpp
84Napi::Error::Error(napi_env env, napi_value value);
85```
86
87- `[in] env`: The environment in which to construct the error object.
88- `[in] value`: The `Napi::Error` reference to wrap.
89
90Returns instance of an `Napi::Error` object.
91
92### Message
93
94```cpp
95std::string& Napi::Error::Message() const NAPI_NOEXCEPT;
96```
97
98Returns the reference to the string that represent the message of the error.
99
100### ThrowAsJavaScriptException
101
102Throw the error as JavaScript exception.
103
104```cpp
105void Napi::Error::ThrowAsJavaScriptException() const;
106```
107
108Throws the error as a JavaScript exception.
109
110### what
111
112```cpp
113const char* Napi::Error::what() const NAPI_NOEXCEPT override;
114```
115
116Returns a pointer to a null-terminated string that is used to identify the
117exception. This method can be used only if the exception mechanism is enabled.
118
119[`Napi::ObjectReference`]: ./object_reference.md
120[`std::exception`]: /reference/exception/exception/
121