You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var exn =Caml_js_exceptions.internalToOCamlException(raw_exn);
337
+
result =Primitive_int.div(10, 0);
338
+
} catch (raw_exn) {
339
+
let exn =Primitive_exceptions.internalToException(raw_exn);
357
340
if (exn.RE_EXN_ID==="Division_by_zero") {
358
341
result =undefined;
359
342
} else {
@@ -368,7 +351,7 @@ console.log(result);
368
351
369
352
## Catching JS Exceptions
370
353
371
-
To distinguish between JavaScript exceptions and ReScript exceptions, ReScript namespaces JS exceptions under the `Exn.Error(payload)` variant. To catch an exception thrown from the JS side:
354
+
To distinguish between JavaScript exceptions and ReScript exceptions, ReScript namespaces JS exceptions under the `JsExn(payload)` variant. To catch an exception thrown from the JS side:
372
355
373
356
374
357
Throw an exception from JS:
@@ -392,32 +375,35 @@ try {
392
375
// call the external method
393
376
someJSFunctionThatThrows()
394
377
} catch {
395
-
| Exn.Error(obj) =>
396
-
switch Exn.message(obj) {
378
+
| JsExn(exn) =>
379
+
switch JsExn.message(exn) {
397
380
| Some(m) => Console.log("Caught a JS exception! Message: " ++ m)
398
381
| None => ()
399
382
}
400
383
}
401
384
```
402
385
403
-
The `obj` here is of type `Exn.t`, intentionally opaque to disallow illegal operations. To operate on `obj`, do like the code above by using the standard library's [`Exn`](api/js/exn) module's helpers.
386
+
The payload `exn` here is of type `unknown` since in JS you can throw anything. To operate on `exn`, do like the code above by using the standard library's [`JsExn`](api/core/jsexn) module's helpers
387
+
or use [`Type.Classify.classify`](api/core/type/classify#value-classify) to get more information about the runtime type of `exn`.
404
388
405
-
## Raise a JS Exception
389
+
## Throw a JS Exception
406
390
407
-
`raise(MyException)` raises a ReScript exception. To raise a JavaScript exception (whatever your purpose is), use `Exn.raiseError`:
391
+
### Throw a JS Error
392
+
393
+
`throw(MyException)` throws a ReScript exception. To throw a JavaScript error (whatever your purpose is), use `JsError.throwWithMessage`:
If you want to throw any value that is not a valid JS Error, use `JsExn.throw`:
426
+
427
+
<CodeTablabels={["ReScript", "JS Output"]}>
428
+
429
+
```res example
430
+
let myTest = () => {
431
+
JsExn.throw("some non-error value!")
432
+
}
433
+
```
434
+
```js
435
+
functionmyTest() {
436
+
throw"some non-error value!";
437
+
}
438
+
```
439
+
440
+
</CodeTab>
441
+
442
+
Then you can catch it from the JS side:
443
+
444
+
```js
445
+
// after importing `myTest`...
446
+
try {
447
+
myTest()
448
+
} catch (message) {
449
+
console.log(message) // "Hello!"
450
+
}
451
+
```
452
+
437
453
## Catch ReScript Exceptions from JS
438
454
439
455
The previous section is less useful than you think; to let your JS code work with your exception-throwing ReScript code, the latter doesn't actually need to throw a JS exception. ReScript exceptions can be used by JS code!
@@ -444,13 +460,13 @@ The previous section is less useful than you think; to let your JS code work wit
444
460
exception BadArgument({myMessage: string})
445
461
446
462
let myTest = () => {
447
-
raise(BadArgument({myMessage: "Oops!"}))
463
+
throw(BadArgument({myMessage: "Oops!"}))
448
464
}
449
465
```
450
466
```js
451
-
var Caml_exceptions =require("./stdlib/caml_exceptions.js");
0 commit comments