• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Error Handling Using JSVM-API
2
3## Introduction
4
5JSVM-API provides APIs for handling errors occurred in JavaScript (JS) code using exceptions. Properly using these APIs helps improve module stability and reliability.
6
7## Basic Concepts
8
9Exceptions and errors are common concepts in JS programming. An exception indicates the presence of an unexpected condition, and an error indicates that the application cannot perform certain operations correctly. JSVM-API provides a set of APIs for handling errors occurred in JS code using exceptions. Read on the following to learn basic concepts related to exception handling:
10
11- Exception: indicates an unexpected condition that may occur during the execution of an application. It can be a syntax error, runtime error, or logic error. For example, the division of a non-zero value with zero and an operation on undefined variables are exceptions.
12- Error: indicates that the application cannot perform some operations. Errors can be defined by the underlying system, API, or developer.
13- **TypeError**: indicates that the type of an operation or value does not meet the expectation. Generally, this error is caused by an incorrect data type.
14- **RangeError**: indicates that a value is not in the expected range. For example, an index beyond the array length is accessed.
15- **SyntaxError**: indicates a mistake in the syntax of a piece of code.
16
17These concepts are important in exception and error handling. Properly using methods to capture, handle, or report exceptions and errors help improve application stability. JSVM-API provides APIs for handling errors in JS code using exceptions.
18
19## Available APIs
20
21| API                      | Description                      |
22|----------------------------|--------------------------------|
23| OH_JSVM_CreateError, OH_JSVM_CreateTypeError, OH_JSVM_CreateRangeError, OH_JSVM_CreateSyntaxError| Creates a JS error.|
24| OH_JSVM_Throw | Throws a JS error object, which is created by **OH_JSVM_CreateError** or obtained by **OH_JSVM_GetLastErrorInfo**.|
25| OH_JSVM_ThrowError, OH_JSVM_ThrowTypeError, OH_JSVM_ThrowRangeError, OH_JSVM_ThrowSyntaxError| Throws a JS error object.|
26| OH_JSVM_IsError              | Checks whether the given **JSVM_Value** indicates an error.|
27| OH_JSVM_GetAndClearLastException    | Obtains and clears the last JS exception.|
28| OH_JSVM_IsExceptionPending   | Checks whether there is any pending exception.|
29| OH_JSVM_GetLastErrorInfo     | Obtains the last error information.|
30
31## Example
32
33If you are just starting out with JSVM-API, see [JSVM-API Development Process](use-jsvm-process.md). The following demonstrates only the C++ code involved in error handling.
34
35### OH_JSVM_Throw
36
37Call **OH_JSVM_Throw** to throw a JS error object. You can use this API to throw a JS error that indicates an error or unexpected behavior occurred in the native code so that exception can be captured and handled. For details about the example, see the example of **OH_JSVM_CreateError**.
38
39### OH_JSVM_CreateError
40
41Call **OH_JSVM_CreateError** to create a JS error object with text information.
42
43CPP code:
44
45```cpp
46// hello.cpp
47// Capture and clear the last exception in the JSVM environment and log the error message. This function is used as a public function and will not be declared or defined in subsequent examples in this document.
48static void GetLastErrorAndClean(JSVM_Env env) {
49    // Call OH_JSVM_GetAndClearLastException to obtain and clear the last exception. This API can also be used to handle a suspended JS exception.
50    JSVM_Value result = nullptr;
51    JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result);
52    // Log error information.
53    JSVM_Value message;
54    JSVM_Value errorCode;
55    OH_JSVM_GetNamedProperty((env), result, "message", &message);
56    OH_JSVM_GetNamedProperty((env), result, "code", &errorCode);
57    char messagestr[256];
58    char codeStr[256];
59    OH_JSVM_GetValueStringUtf8(env, message, messagestr, 256, nullptr);
60    OH_JSVM_GetValueStringUtf8(env, errorCode, codeStr, 256, nullptr);
61    OH_LOG_INFO(LOG_APP, "JSVM error message: %{public}s, error code: %{public}s", messagestr, codeStr);
62}
63
64// Define OH_JSVM_CreateError.
65static JSVM_Value JsVmCreateThrowError(JSVM_Env env, JSVM_CallbackInfo info) {
66    // Create a string in the JSVM and store it in the errorCode variable.
67    JSVM_Value errorCode = nullptr;
68    OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
69    // Create a string in the JSVM and store it in the errorMessage variable.
70    JSVM_Value errorMessage = nullptr;
71    OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
72    // Create a JS error object.
73    JSVM_Value error = nullptr;
74    OH_JSVM_CreateError(env, errorCode, errorMessage, &error);
75    // Call OH_JSVM_Throw to throw the error.
76    OH_JSVM_Throw(env, error);
77    GetLastErrorAndClean(env);
78    return nullptr;
79}
80
81// Register the JsVmCreateThrowError callback.
82static JSVM_CallbackStruct param[] = {
83    {.data = nullptr, .callback = JsVmCreateThrowError},
84};
85static JSVM_CallbackStruct *method = param;
86// Alias for the JsVmCreateThrowError method to be called from JS.
87static JSVM_PropertyDescriptor descriptor[] = {
88    {"jsVmCreateThrowError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
89};
90// Call the C++ code from JS.
91const char *srcCallNative = R"JS(jsVmCreateThrowError();)JS";
92```
93**Expected output**
94```ts
95JSVM error message: HasError, error code: -1
96```
97
98### OH_JSVM_ThrowError
99
100Call **OH_JSVM_ThrowError** to throw a JS **Error** object with text information.
101
102CPP code:
103
104```cpp
105// hello.cpp
106// Define OH_JSVM_ThrowError.
107static JSVM_Value JsVmThrowError(JSVM_Env env, JSVM_CallbackInfo info)
108{
109    size_t argc = 1;
110    JSVM_Value argv[1] = {nullptr};
111    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
112    if (argc == 0) {
113        // Throw an error if no parameter is passed in.
114        OH_JSVM_ThrowError(env, "-1", "has Error");
115    } else if (argc == 1) {
116        size_t length;
117        // Obtain the length of the string passed from JS from the input parameter.
118        OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
119        char *buffer = new char[length + 1];
120        // Obtain the string of the input parameter.
121        OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
122        // Populate the error information to OH_JSVM_ThrowError.
123        OH_JSVM_ThrowError(env, "self defined error code", buffer);
124        delete[] buffer;
125    }
126    GetLastErrorAndClean(env);
127    return nullptr;
128}
129// Register the JsVmThrowError callback.
130static JSVM_CallbackStruct param[] = {
131    {.data = nullptr, .callback = JsVmThrowError},
132};
133static JSVM_CallbackStruct *method = param;
134// Alias for the JsVmThrowError method to be called from JS.
135static JSVM_PropertyDescriptor descriptor[] = {
136    {"jsVmThrowError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
137};
138// Call the C++ code from JS.
139const char *srcCallNative = R"JS(jsVmThrowError();jsVmThrowError("self defined error message");)JS";
140```
141
142**Expected output**
143```ts
144JSVM error message: has Error, error code: -1
145JSVM error message: self defined error message, error code: self defined error code
146```
147
148### OH_JSVM_ThrowTypeError
149
150Call **OH_JSVM_CreateTypeError** to create a JS **TypeError** object with text information.
151
152CPP code:
153
154```cpp
155// hello.cpp
156// Define OH_JSVM_ThrowTypeError.
157static JSVM_Value JsVmThrowTypeError(JSVM_Env env, JSVM_CallbackInfo info) {
158    size_t argc = 1;
159    JSVM_Value argv[1] = {nullptr};
160    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
161    if (argc == 0) {
162        // Throw an error if no parameter is passed in.
163        OH_JSVM_ThrowTypeError(env, "-1", "throwing type error");
164    } else if (argc == 1) {
165        size_t length;
166        // Obtain the length of the string in the input parameter passed from JS.
167        OH_JSVM_GetValueStringUtf8(env, argv[0], nullptr, 0, &length);
168        char *buffer = new char[length + 1];
169        // Obtain the string of the input parameter.
170        OH_JSVM_GetValueStringUtf8(env, argv[0], buffer, length + 1, nullptr);
171        // Populate the error information to OH_JSVM_ThrowError.
172        OH_JSVM_ThrowTypeError(env, "self defined error code", buffer);
173        delete[] buffer;
174    }
175    GetLastErrorAndClean(env);
176    return nullptr;
177}
178// Register the **JsVmThrowTypeError** callback.
179static JSVM_CallbackStruct param[] = {
180    {.data = nullptr, .callback = JsVmThrowTypeError},
181};
182static JSVM_CallbackStruct *method = param;
183// Alias for the JsVmThrowTypeError method to be called from JS.
184static JSVM_PropertyDescriptor descriptor[] = {
185    {"jsVmThrowTypeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
186};
187// Call the C++ code from JS.
188const char *srcCallNative = R"JS(jsVmThrowTypeError();jsVmThrowTypeError("self defined error message");)JS";
189```
190
191**Expected output**
192```ts
193JSVM error message: throwing type error, error code: -1
194JSVM error message: self defined error message, error code: self defined error code
195```
196
197### OH_JSVM_ThrowRangeError
198
199Call **OH_JSVM_CreateRangeError** to create a JS **RangeError** with text information.
200
201CPP code:
202
203```cpp
204// hello.cpp
205// Define OH_JSVM_ThrowRangeError.
206static JSVM_Value JsVmThrowRangeError(JSVM_Env env, JSVM_CallbackInfo info)
207{
208    // Pass two parameters from JS.
209    size_t argc = 2;
210    JSVM_Value argv[2] = {nullptr};
211    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
212    // If the number of parameters is not 2,
213    if (argc != 2) {
214        // Throw a RangeError.
215        OH_JSVM_ThrowRangeError(env, "OH_JSVM_ThrowRangeError", "Expected two numbers as arguments");
216        GetLastErrorAndClean(env);
217        return nullptr;
218    }
219    JSVM_Value result = nullptr;
220    OH_JSVM_GetBoolean(env, true, &result);
221    return result;
222}
223// Register the JsVmThrowRangeError callback.
224static JSVM_CallbackStruct param[] = {
225    {.data = nullptr, .callback = JsVmThrowRangeError},
226};
227static JSVM_CallbackStruct *method = param;
228// Alias for the JsVmThrowRangeError method to be called from JS.
229static JSVM_PropertyDescriptor descriptor[] = {
230    {"jsVmThrowRangeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
231};
232// Call the C++ code from JS.
233const char *srcCallNative = R"JS(jsVmThrowRangeError(1);)JS";
234```
235
236
237**Expected output**
238```ts
239JSVM error message: Expected two numbers as arguments, error code: OH_JSVM_ThrowRangeError
240```
241
242### OH_JSVM_ThrowSyntaxError
243
244Call **OH_JSVM_ThrowSyntaxError** to create and throw a JS **SyntaxError** object with text information.
245
246CPP code:
247
248```cpp
249// hello.cpp
250// Define OH_JSVM_ThrowSyntaxError.
251static JSVM_Value JsVmThrowSyntaxError(JSVM_Env env, JSVM_CallbackInfo info) {
252    // Pass the JS code from JS.
253    size_t argc = 1;
254    JSVM_Value argv[1] = {nullptr};
255    OH_JSVM_GetCbInfo(env, info, &argc, argv, nullptr, nullptr);
256    JSVM_Script script = nullptr;
257    // Call OH_JSVM_CompileScript to compile the JS code.
258    OH_JSVM_CompileScript(env, argv[0], nullptr, 0, true, nullptr, &script);
259    JSVM_Value scriptResult = nullptr;
260    // Call OH_JSVM_RunScript to run the JS code.
261    JSVM_Status status = OH_JSVM_RunScript(env, script, &scriptResult);
262    if (status != JSVM_OK) {
263        // If the value returned by JSVM_RunScript is not JSVM_OK, throw a SyntaxError.
264        OH_JSVM_ThrowSyntaxError(env, "JsVmThrowSyntaxError", "throw syntax error");
265        GetLastErrorAndClean(env);
266        return nullptr;
267    }
268    JSVM_Value result = nullptr;
269    OH_JSVM_GetBoolean(env, true, &result);
270    return result;
271}
272// Register the JsVmThrowSyntaxError callback.
273static JSVM_CallbackStruct param[] = {
274    {.data = nullptr, .callback = JsVmThrowSyntaxError},
275};
276static JSVM_CallbackStruct *method = param;
277// Alias for the JsVmThrowSyntaxError method to be called from JS.
278static JSVM_PropertyDescriptor descriptor[] = {
279    {"jsVmThrowSyntaxError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
280};
281// Call the C++ code from JS.
282const char *srcCallNative = R"JS(jsVmThrowSyntaxError();)JS";
283```
284
285**Expected output**
286```ts
287JSVM error message: throw syntax error, error code: JsVmThrowSyntaxError
288```
289
290### OH_JSVM_IsError
291
292Call **OH_JSVM_IsError** to check whether the given **JSVM_Value** represents an error object.
293
294CPP code:
295
296```cpp
297// hello.cpp
298// Define OH_JSVM_IsError.
299static JSVM_Value JsVmIsError(JSVM_Env env, JSVM_CallbackInfo info) {
300    size_t argc = 1;
301    JSVM_Value args[1] = {nullptr};
302    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
303    // Call OH_JSVM_IsError to check whether the input parameter is an error object.
304    bool result = false;
305    // If JSVM_Value is an error object, set result to true. Otherwise, set result to false.
306    JSVM_Status status = OH_JSVM_IsError(env, args[0], &result);
307    if (status == JSVM_OK) {
308        OH_LOG_INFO(LOG_APP, "JSVM API call OH_JSVM_IsError success, result is %{public}d", result);
309    }else {
310        OH_LOG_INFO(LOG_APP, "JSVM API call OH_JSVM_IsError failed");
311    }
312    // Obtain result, call OH_JSVM_GetBoolean to convert result to JSVM_Value, and return JSVM_Value.
313    JSVM_Value returnValue = nullptr;
314    OH_JSVM_GetBoolean(env, result, &returnValue);
315    return returnValue;
316}
317// Register the JsVmIsError callback.
318static JSVM_CallbackStruct param[] = {
319    {.data = nullptr, .callback = JsVmIsError},
320};
321static JSVM_CallbackStruct *method = param;
322// Alias for the JsVmIsError method to be called from JS.
323static JSVM_PropertyDescriptor descriptor[] = {
324    {"jsVmIsError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
325};
326// Call the C++ code from JS.
327const char *srcCallNative = R"JS(jsVmIsError(Error()))JS";
328```
329
330**Expected output**
331```ts
332JSVM API call OH_JSVM_IsError success, result is 1
333```
334
335### OH_JSVM_CreateTypeError
336
337Call **OH_JSVM_CreateTypeError** to create a JS **TypeError** object with text information.
338
339CPP code:
340
341```cpp
342// hello.cpp
343// Define OH_JSVM_CreateTypeError.
344static JSVM_Value JsVmCreateTypeError(JSVM_Env env, JSVM_CallbackInfo info) {
345    // Create a string in the JSVM and store it in the errorCode variable.
346    JSVM_Value errorCode = nullptr;
347    OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
348    // Create a string in the JSVM and store it in the errorMessage variable.
349    JSVM_Value errorMessage = nullptr;
350    OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
351    JSVM_Value result = nullptr;
352    JSVM_Status status = OH_JSVM_CreateTypeError(env, errorCode, errorMessage, &result);
353    if (status == JSVM_OK) {
354        OH_LOG_INFO(LOG_APP, "JSVM API Create TypeError SUCCESS");
355    } else {
356        OH_LOG_INFO(LOG_APP, "JSVM API Create TypeError FAILED");
357    }
358    return result;
359}
360// Register the JsVmCreateTypeError callback.
361static JSVM_CallbackStruct param[] = {
362    {.data = nullptr, .callback = JsVmCreateTypeError},
363};
364static JSVM_CallbackStruct *method = param;
365// Alias for the JsVmCreateTypeError method to be called from JS.
366static JSVM_PropertyDescriptor descriptor[] = {
367    {"jsVmCreateTypeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
368};
369// Call the C++ code from JS.
370const char *srcCallNative = R"JS(jsVmCreateTypeError();)JS";
371```
372
373**Expected output**
374```ts
375JSVM API Create TypeError SUCCESS
376```
377
378### OH_JSVM_CreateRangeError
379
380Call **OH_JSVM_CreateRangeError** to create a JS **RangeError** with text information.
381
382CPP code:
383
384```cpp
385// hello.cpp
386// Define OH_JSVM_CreateRangeError.
387static JSVM_Value JsVmCreateRangeError(JSVM_Env env, JSVM_CallbackInfo info) {
388    // Create a string in the JSVM and store it in the errorCode variable.
389    JSVM_Value errorCode = nullptr;
390    OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
391    // Create a string in the JSVM and store it in the errorMessage variable.
392    JSVM_Value errorMessage = nullptr;
393    OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
394    JSVM_Value result = nullptr;
395    JSVM_Status status = OH_JSVM_CreateRangeError(env, errorCode, errorMessage, &result);
396    if (status == JSVM_OK) {
397        OH_LOG_INFO(LOG_APP, "JSVM API CreateRangeError SUCCESS");
398    } else {
399        OH_LOG_INFO(LOG_APP, "JSVM API CreateRangeError FAILED");
400    }
401    return result;
402}
403// Register the JsVmCreateRangeError callback.
404static JSVM_CallbackStruct param[] = {
405    {.data = nullptr, .callback = JsVmCreateRangeError},
406};
407static JSVM_CallbackStruct *method = param;
408// Alias for the JsVmCreateRangeError method to be called from JS.
409static JSVM_PropertyDescriptor descriptor[] = {
410    {"jsVmCreateRangeError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
411};
412// Call the C++ code from JS.
413const char *srcCallNative = R"JS(jsVmCreateRangeError();)JS";
414```
415
416**Expected output**
417```ts
418JSVM API CreateRangeError SUCCESS
419```
420### OH_JSVM_CreateSyntaxError
421
422Call **OH_JSVM_CreateSyntaxError** to create and throw a JS **SyntaxError** object with text information.
423
424CPP code:
425
426```cpp
427// hello.cpp
428// Define OH_JSVM_CreateSyntaxError.
429static JSVM_Value JsVmCreateSyntaxError(JSVM_Env env, JSVM_CallbackInfo info) {
430    // Create a string in the JSVM and store it in the errorCode variable.
431    JSVM_Value errorCode = nullptr;
432    OH_JSVM_CreateStringUtf8(env, "-1", JSVM_AUTO_LENGTH, &errorCode);
433    // Create a string in the JSVM and store it in the errorMessage variable.
434    JSVM_Value errorMessage = nullptr;
435    OH_JSVM_CreateStringUtf8(env, "HasError", JSVM_AUTO_LENGTH, &errorMessage);
436    JSVM_Value result = nullptr;
437    JSVM_Status status =  OH_JSVM_CreateSyntaxError(env, errorCode, errorMessage, &result);
438    if (status == JSVM_OK) {
439        OH_LOG_INFO(LOG_APP, "JSVM API CreateSyntaxError SUCCESS");
440    } else {
441        OH_LOG_INFO(LOG_APP, "JSVM API CreateSyntaxError FAILED");
442    }
443    return result;
444}
445// Register the JsVmCreateSyntaxError callback.
446static JSVM_CallbackStruct param[] = {
447    {.data = nullptr, .callback = JsVmCreateSyntaxError},
448};
449static JSVM_CallbackStruct *method = param;
450// Alias for the JsVmCreateThrowError method to be called from JS.
451static JSVM_PropertyDescriptor descriptor[] = {
452    {"jsVmCreateSyntaxError", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
453};
454// Call the C++ code from JS.
455const char *srcCallNative = R"JS(jsVmCreateSyntaxError();)JS";
456```
457
458**Expected output**
459```ts
460JSVM API CreateSyntaxError SUCCESS
461```
462
463### OH_JSVM_GetAndClearLastException
464
465Call **OH_JSVM_GetAndClearLastException** to obtain and clear the latest exception.
466
467CPP code:
468
469```cpp
470// hello.cpp
471// Define OH_JSVM_GetAndClearLastException.
472static JSVM_Value JsVmGetAndClearLastException(JSVM_Env env, JSVM_CallbackInfo info) {
473    // Throw an error.
474    OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError errorCode", "OH_JSVM_ThrowError errorMessage");
475    // Call OH_JSVM_GetAndClearLastException to obtain and clear the last exception. This API can also be used to handle a suspended JS exception.
476    JSVM_Value result = nullptr;
477    JSVM_Status status = OH_JSVM_GetAndClearLastException(env, &result);
478    if (status != JSVM_OK) {
479        OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_GetAndClearLastException FAILED");
480    } else {
481        OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_GetAndClearLastException SUCCESS");
482    }
483    return result;
484}
485// Register the JsVmGetAndClearLastException callback.
486static JSVM_CallbackStruct param[] = {
487    {.data = nullptr, .callback = JsVmGetAndClearLastException},
488};
489static JSVM_CallbackStruct *method = param;
490// Alias for the JsVmGetAndClearLastException method to be called from JS.
491static JSVM_PropertyDescriptor descriptor[] = {
492    {"jsVmGetAndClearLastException", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
493};
494// Call the C++ code from JS.
495const char *srcCallNative = R"JS(jsVmGetAndClearLastException();)JS";
496```
497
498**Expected output**
499```ts
500JSVM API OH_JSVM_GetAndClearLastException SUCCESS
501```
502
503### OH_JSVM_IsExceptionPending
504
505Call **OH_JSVM_IsExceptionPending** to check whether there is any pending exception.
506
507CPP code:
508
509```cpp
510// hello.cpp
511// Define OH_JSVM_GetAndClearLastException.
512static JSVM_Value JsVmIsExceptionPending(JSVM_Env env, JSVM_CallbackInfo info) {
513    JSVM_Status status;
514    bool isExceptionPending = false;
515    // Perform operations that may cause an error.
516    OH_JSVM_ThrowError(env, "OH_JSVM_ThrowError errorCode", "OH_JSVM_ThrowError errorMessage");
517    // Check whether there is a pending exception.
518    status = OH_JSVM_IsExceptionPending(env, &isExceptionPending);
519    if (status != JSVM_OK) {
520        return nullptr;
521    }
522    if (isExceptionPending) {
523        OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_IsExceptionPending: SUCCESS");
524        // Handle the pending exception.
525        JSVM_Value result = nullptr;
526        status = OH_JSVM_GetAndClearLastException(env, &result);
527        if (status != JSVM_OK) {
528            return nullptr;
529        }
530        // Return the result.
531        return result;
532    } else {
533        OH_LOG_INFO(LOG_APP, "JSVM API OH_JSVM_IsExceptionPending: FAILED");
534    }
535    return nullptr;
536}
537// Register the JsVmIsExceptionPending callback.
538static JSVM_CallbackStruct param[] = {
539    {.data = nullptr, .callback = JsVmIsExceptionPending},
540};
541static JSVM_CallbackStruct *method = param;
542// Alias for the JsVmIsExceptionPending method to be called from JS.
543static JSVM_PropertyDescriptor descriptor[] = {
544    {"jsVmIsExceptionPending", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
545};
546// Call the C++ code from JS.
547const char *srcCallNative = R"JS(jsVmIsExceptionPending();)JS";
548```
549
550**Expected output**
551```ts
552JSVM API OH_JSVM_IsExceptionPending: SUCCESS
553```
554
555### OH_JSVM_GetLastErrorInfo
556
557Call **OH_JSVM_GetLastErrorInfo** to obtain the last error information (the return value is not **JSVM_OK**), including the error code, error message, and stack information. This API can also be used for suspended JS errors.
558Note that the errors triggered by APIs such as **OH_JSVM_ThrowError** will not be captured by the APIs unless the return value is not **JSVM_OK**.
559
560CPP code:
561
562```cpp
563// hello.cpp
564// Define OH_JSVM_GetLastErrorInfo.
565static JSVM_Value JsVmGetLastErrorInfo(JSVM_Env env, JSVM_CallbackInfo info) {
566    // Obtain the input parameter, that is the message string in this example.
567    size_t argc = 1;
568    JSVM_Value args[1] = {nullptr};
569    OH_JSVM_GetCbInfo(env, info, &argc, args, nullptr, nullptr);
570    // Call OH_JSVM_GetValueInt32 to obtain the input string parameter to create an error.
571    int32_t value = 0;
572    OH_JSVM_GetValueInt32(env, args[0], &value);
573    // Call OH_JSVM_GetLastErrorInfo to obtain the last error message.
574    const JSVM_ExtendedErrorInfo *errorInfo;
575    OH_JSVM_GetLastErrorInfo(env, &errorInfo);
576
577    // Obtain the error message as the return value and print it.
578    JSVM_Value result = nullptr;
579    OH_LOG_INFO(LOG_APP,
580                "JSVM API OH_JSVM_GetLastErrorInfo: SUCCESS, error message is %{public}s, error code is %{public}d",
581                errorInfo->errorMessage, errorInfo->errorCode);
582    // Handle the exception to prevent the application from exiting due to the error thrown.
583    JSVM_Value result1 = nullptr;
584    OH_JSVM_GetAndClearLastException(env, &result1);
585    OH_JSVM_CreateInt32(env, errorInfo->errorCode, &result);
586    return result;
587}
588// Register the JsVmGetLastErrorInfo callback.
589static JSVM_CallbackStruct param[] = {
590    {.data = nullptr, .callback = JsVmGetLastErrorInfo},
591};
592static JSVM_CallbackStruct *method = param;
593// Alias for the JsVmGetLastErrorInfo method to be called from JS.
594static JSVM_PropertyDescriptor descriptor[] = {
595    {"jsVmGetLastErrorInfo", nullptr, method++, nullptr, nullptr, nullptr, JSVM_DEFAULT},
596};
597// Call the C++ code from JS.
598const char *srcCallNative = R"JS(jsVmGetLastErrorInfo();)JS";
599```
600
601**Expected output**
602```ts
603JSVM API OH_JSVM_GetLastErrorInfo: SUCCESS, error message is A number was expected, error code is 6
604```
605