18c339a94Sopenharmony_ci# Error
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciClass `Napi::Error` inherits from class [`Napi::ObjectReference`][] and class [`std::exception`][].
48c339a94Sopenharmony_ci
58c339a94Sopenharmony_ciThe `Napi::Error` class is a representation of the JavaScript `Error` object that is thrown
68c339a94Sopenharmony_ciwhen runtime errors occur. The Error object can also be used as a base object for
78c339a94Sopenharmony_ciuser-defined exceptions.
88c339a94Sopenharmony_ci
98c339a94Sopenharmony_ciThe `Napi::Error` class is a persistent reference to a JavaScript error object thus
108c339a94Sopenharmony_ciinherits its behavior from the `Napi::ObjectReference` class (for more info see: [`Napi::ObjectReference`](object_reference.md)).
118c339a94Sopenharmony_ci
128c339a94Sopenharmony_ciIf C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
138c339a94Sopenharmony_ci`Napi::Error` class extends `std::exception` and enables integrated
148c339a94Sopenharmony_cierror-handling for C++ exceptions and JavaScript exceptions.
158c339a94Sopenharmony_ci
168c339a94Sopenharmony_ciFor more details about error handling refer to the section titled [Error handling](error_handling.md).
178c339a94Sopenharmony_ci
188c339a94Sopenharmony_ci## Methods
198c339a94Sopenharmony_ci
208c339a94Sopenharmony_ci### New
218c339a94Sopenharmony_ci
228c339a94Sopenharmony_ciCreates empty instance of an `Napi::Error` object for the specified environment.
238c339a94Sopenharmony_ci
248c339a94Sopenharmony_ci```cpp
258c339a94Sopenharmony_ciNapi::Error::New(Napi::Env env);
268c339a94Sopenharmony_ci```
278c339a94Sopenharmony_ci
288c339a94Sopenharmony_ci- `[in] env`: The environment in which to construct the `Napi::Error` object.
298c339a94Sopenharmony_ci
308c339a94Sopenharmony_ciReturns an instance of `Napi::Error` object.
318c339a94Sopenharmony_ci
328c339a94Sopenharmony_ci### New
338c339a94Sopenharmony_ci
348c339a94Sopenharmony_ciCreates instance of an `Napi::Error` object.
358c339a94Sopenharmony_ci
368c339a94Sopenharmony_ci```cpp
378c339a94Sopenharmony_ciNapi::Error::New(Napi::Env env, const char* message);
388c339a94Sopenharmony_ci```
398c339a94Sopenharmony_ci
408c339a94Sopenharmony_ci- `[in] env`: The environment in which to construct the `Napi::Error` object.
418c339a94Sopenharmony_ci- `[in] message`: Null-terminated string to be used as the message for the `Napi::Error`.
428c339a94Sopenharmony_ci
438c339a94Sopenharmony_ciReturns instance of an `Napi::Error` object.
448c339a94Sopenharmony_ci
458c339a94Sopenharmony_ci### New
468c339a94Sopenharmony_ci
478c339a94Sopenharmony_ciCreates instance of an `Napi::Error` object
488c339a94Sopenharmony_ci
498c339a94Sopenharmony_ci```cpp
508c339a94Sopenharmony_ciNapi::Error::New(Napi::Env env, const std::string& message);
518c339a94Sopenharmony_ci```
528c339a94Sopenharmony_ci
538c339a94Sopenharmony_ci- `[in] env`: The environment in which to construct the `Napi::Error` object.
548c339a94Sopenharmony_ci- `[in] message`: Reference string to be used as the message for the `Napi::Error`.
558c339a94Sopenharmony_ci
568c339a94Sopenharmony_ciReturns instance of an `Napi::Error` object.
578c339a94Sopenharmony_ci
588c339a94Sopenharmony_ci### Fatal
598c339a94Sopenharmony_ci
608c339a94Sopenharmony_ciIn case of an unrecoverable error in a native module, a fatal error can be thrown
618c339a94Sopenharmony_cito immediately terminate the process.
628c339a94Sopenharmony_ci
638c339a94Sopenharmony_ci```cpp
648c339a94Sopenharmony_cistatic NAPI_NO_RETURN void Napi::Error::Fatal(const char* location, const char* message);
658c339a94Sopenharmony_ci```
668c339a94Sopenharmony_ci
678c339a94Sopenharmony_ciThe function call does not return, the process will be terminated.
688c339a94Sopenharmony_ci
698c339a94Sopenharmony_ci### Constructor
708c339a94Sopenharmony_ci
718c339a94Sopenharmony_ciCreates empty instance of an `Napi::Error`.
728c339a94Sopenharmony_ci
738c339a94Sopenharmony_ci```cpp
748c339a94Sopenharmony_ciNapi::Error::Error();
758c339a94Sopenharmony_ci```
768c339a94Sopenharmony_ci
778c339a94Sopenharmony_ciReturns an instance of `Napi::Error` object.
788c339a94Sopenharmony_ci
798c339a94Sopenharmony_ci### Constructor
808c339a94Sopenharmony_ci
818c339a94Sopenharmony_ciInitializes an `Napi::Error` instance from an existing JavaScript error object.
828c339a94Sopenharmony_ci
838c339a94Sopenharmony_ci```cpp
848c339a94Sopenharmony_ciNapi::Error::Error(napi_env env, napi_value value);
858c339a94Sopenharmony_ci```
868c339a94Sopenharmony_ci
878c339a94Sopenharmony_ci- `[in] env`: The environment in which to construct the error object.
888c339a94Sopenharmony_ci- `[in] value`: The `Napi::Error` reference to wrap.
898c339a94Sopenharmony_ci
908c339a94Sopenharmony_ciReturns instance of an `Napi::Error` object.
918c339a94Sopenharmony_ci
928c339a94Sopenharmony_ci### Message
938c339a94Sopenharmony_ci
948c339a94Sopenharmony_ci```cpp
958c339a94Sopenharmony_cistd::string& Napi::Error::Message() const NAPI_NOEXCEPT;
968c339a94Sopenharmony_ci```
978c339a94Sopenharmony_ci
988c339a94Sopenharmony_ciReturns the reference to the string that represent the message of the error.
998c339a94Sopenharmony_ci
1008c339a94Sopenharmony_ci### ThrowAsJavaScriptException
1018c339a94Sopenharmony_ci
1028c339a94Sopenharmony_ciThrow the error as JavaScript exception.
1038c339a94Sopenharmony_ci
1048c339a94Sopenharmony_ci```cpp
1058c339a94Sopenharmony_civoid Napi::Error::ThrowAsJavaScriptException() const;
1068c339a94Sopenharmony_ci```
1078c339a94Sopenharmony_ci
1088c339a94Sopenharmony_ciThrows the error as a JavaScript exception.
1098c339a94Sopenharmony_ci
1108c339a94Sopenharmony_ci### what
1118c339a94Sopenharmony_ci
1128c339a94Sopenharmony_ci```cpp
1138c339a94Sopenharmony_ciconst char* Napi::Error::what() const NAPI_NOEXCEPT override;
1148c339a94Sopenharmony_ci```
1158c339a94Sopenharmony_ci
1168c339a94Sopenharmony_ciReturns a pointer to a null-terminated string that is used to identify the
1178c339a94Sopenharmony_ciexception. This method can be used only if the exception mechanism is enabled.
1188c339a94Sopenharmony_ci
1198c339a94Sopenharmony_ci[`Napi::ObjectReference`]: ./object_reference.md
1208c339a94Sopenharmony_ci[`std::exception`]: /reference/exception/exception/
121