Subj : Re: Throwing host exception from host object To : Larry Blanchette From : Igor Bukanov Date : Wed Mar 17 2004 11:25 am Larry Blanchette wrote: > In rhino, I would like throw from a host object my own scriptable types as > catchable exceptions of "my type" in javascript. I seems that > > extending EvaluatorException > > and using > > Context.throwAsScriptRuntimeEx(ex); > > is the path to follow but it does not allow me to propagate my type. The > exception is caught as either "internalError" in the case of an evaluator > exception and "JavaException" otherwise. Do I need to extend ScriptRuntime > and override getCatchObject(Context cx, Scriptable scope, Throwable t) to > handle and convert my exceptions (didn't want to go there)? > > So if have > > class MyExceptionType extends ScriptableObject > > and in host object I throw > > Context.throwAsScriptRuntimeEx(myScriptable); // illegal > > > then i would > > try { > host.causeExceptions(); > } catch(exception) { > if(excetion instanceof MyExceptionType) { > // got it > } > } To the desired functionality, use throw new JavaScriptException(myScriptable, "native-method", 1) which will throw your scriptable in the same way as the throw statement in JS does. If you can not add JavaScriptException to the method declaration, then you can replace that line by Context.throwAsScriptRuntimeEx(new JavaScriptException(myScriptable, "native-method", 1)) That wraps JavaScriptException into RuntimeException and Rhino should unwrap it properly and feed to catch in scripts. Regards, Igor .