Catch 'exception' from js!?

It currently appears (I'm not 100% sure):

  1. when a js! block throws an exception, this gets converted to a panic! on the Rust side

  2. this doesn't end up crashing the entire program (somehow?), as it seems to only "burn down to the most recent callback handler"

  3. however, it is often the case that RefCell's that are borrow or borrow_mut retains their borrow/borrow_mut count

  4. Thus, on the next time a callback is called (gui event, request animation frame), we hit a "already mutably borrowed" error


Now, my question is -- can we change (1) ? When js! throws an exception, I would prefer the entire js!{} block to return an Result::Err instead of causing a panic!().

One option is to catch the panic.

1 Like
  1. I thought (perhaps incorrectly) that Result = recoverable errors, panic! = fatal errors.

  2. This does suggest another approach: put the js code in a try/catch block, so the js! won't throw exception.

Catching a panic is an anti-pattern for error handling, but if the library provides no other method, then I don't see any other option. Usually catching panics is done by things like thread pools so they can avoid bringing down the thread on panic.

Additionally if your binary has panic = 'abort' in the appropriate Cargo.toml section, panics will immediately abort the program, so catch_unwind will not work.

Based on this I recommend the try catch block in javascript instead.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.