swimos/swim-java-bindings

Add exception types

Opened this issue · 0 comments

Implement an exception type that can be used for throwing exceptions in the rust runtime.

trait JavaException {
    type Exception<'l, 'c>: Desc<'l, JClass<'c>>;

    fn lookup(env:&JniEnv) -> Result<Self::Exception, JniError>;
}

Which would allow Scope::throw_new to accept JavaException as a type parameter:

pub fn throw_new<E>(&self, msg: S)
where
    E: JavaException
{
    let Scope { vm, env, .. } = self;
    system_call(vm, || {
        let desc = E::lookup(env)?;
        env.throw_new(desc, msg)
    })
}

Usage as:

struct RuntimeException;
impl JavaException for RuntimeException {
...
}

scope.throw_new::<RuntimeException>("...");