Debian Codegraph @

.cargo/vendor/wasm-bindgen/guide/src/reference/attributes/on-js-imports/catch.md

raw
1 # `catch`
2
3 The `catch` attribute allows catching a JavaScript exception. This can be
4 attached to any imported function or method, and the function must return a
5 `Result` where the `Err` payload is a `JsValue`:
6
7 ```rust
8 #[wasm_bindgen]
9 extern "C" {
10 // `catch` on a standalone function.
11 #[wasm_bindgen(catch)]
12 fn foo() -> Result<(), JsValue>;
13
14 // `catch` on a method.
15 type Zoidberg;
16 #[wasm_bindgen(catch, method)]
17 fn woop_woop_woop(this: &Zoidberg) -> Result<u32, JsValue>;
18 }
19 ```
20
21 If calling the imported function throws an exception, then `Err` will be
22 returned with the exception that was raised. Otherwise, `Ok` is returned with
23 the result of the function.
24
25 > By default `wasm-bindgen` will take no action when wasm calls a JS function
26 > which ends up throwing an exception. The wasm spec right now doesn't support
27 > stack unwinding and as a result Rust code **will not execute destructors**.
28 > This can unfortunately cause memory leaks in Rust right now, but as soon as
29 > wasm implements catching exceptions we'll be sure to add support as well!