在 JRuby 中抛出 Java 异常并在 Java 中捕获它
我用 Java 创建了自己的 UI 组件。它有模型,并且模型的一些方法可以抛出称为 ModelException 的异常。我想在 JRuby 中使用这个组件,但我无法引发 ModelException:
raise ModelException # it cause TypeError: exception class/object expected
所以我尝试在 Java 中创建抛出 ModelException 的方法,然后在 JRuby 中调用它:
public class ScriptUtils {
private ScriptUtils() {
}
public static void throwModelException(ModelException e)
throws ModelException {
throw e;
}
}
但是当我从 JRuby 调用 throwModelException 时,我得到:
org.jruby.exceptions.RaiseException: Native Exception: 'class app.ui.ModelException'; Message:
; StackTrace: app.ui.ModelException
...
Caused by: app.ui.ModelException
这个本机异常无法处理通过Java代码。
有什么想法如何在 JRuby 中抛出 Java 异常并在 Java 中捕获它吗?
I have created my own UI component in Java. It has model and a few of model's methods can throw my exception called ModelException. I want to use this component in JRuby but I can't raise my ModelException:
raise ModelException # it cause TypeError: exception class/object expected
So I tried to create method throwing ModelException in Java and then invoke it in JRuby:
public class ScriptUtils {
private ScriptUtils() {
}
public static void throwModelException(ModelException e)
throws ModelException {
throw e;
}
}
but when I call throwModelException from JRuby I get:
org.jruby.exceptions.RaiseException: Native Exception: 'class app.ui.ModelException'; Message:
; StackTrace: app.ui.ModelException
...
Caused by: app.ui.ModelException
this native exception cannot be handled by Java code.
Any ideas how to throw Java exception in JRuby and catch it in Java?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对我原来答案的完全重写,因为我最初误读了你的问题!
您可以引发 Java 异常并在 JRuby 中捕获它们,但您需要小心如何调用 raise:
会导致类型错误(如您所见),因为对于 JRuby
ModelException
来说,它看起来像一个普通常量。请记住,在 Ruby 中,类名是常量。您可以像这样引发 RubyException
的直接子类,例如:但我认为这样的子类是一种特殊情况。那些实际上是 Java 类,您需要使用构造函数来调用:
或者您为该类拥有的任何构造函数。 JRuby 中的
ModelException
实例是Exception
的子类,因为 JRuby 是这样创建它的,但 Java 类本身不是。所有这一切都假设您已正确导入ModelException
类。至于你的第二个例子,我根本无法复制该错误。只要我正确地创建了异常对象,如上所述,它就可以工作,而且我根本没有看到任何关于“本机异常”的抱怨。所以我不确定那里发生了什么,抱歉。
This is a complete re-write of my original answer as I originally mis-read your question!
You can raise Java exceptions and catch them in JRuby but you need to be a bit careful how you call raise:
Will cause a type error (as you saw) because to JRuby
ModelException
looks like a plain constant. Remember that in Ruby class names are constants. You can raise direct subclasses of RubyException
like this, e.g.:But I think such subclasses are a special case. Those that are actually Java classes you need to call with a constructor:
Or whatever constructor/s you have for that class. An instance of
ModelException
, in JRuby, is a subclass ofException
because JRuby creates it as such, but the Java class itself isn't. All this is assuming that you have imported yourModelException
class correctly.As for your second example, I couldn't replicate that error at all. As long as I created the exception object correctly, as above, it worked and I didn't see any complaints about "Native Exception" at all. So I'm not sure what is happening there, sorry.