未报告的异常javax.script.ScriptException;必须被抓住或宣布被抛出 [英] Unreported exception javax.script.ScriptException; must be caught or declared to be thrown
问题描述
这是一个计算器应用程序。当我尝试编译它出现以下消息未报告的异常javax.script.ScriptException;必须被抓住或声明被抛出
我有一种感觉是因为该类设置为 ActionEvent
。我是一个学生,所以我还在学习:)
任何想法?感谢
\
您可以执行以下操作: / p>
// ..其他导入
import javax.script.ScriptException;
// ..其余代码
if(source == buteq){
try {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName(JavaScript);
Object result = se.eval(createEquasion);
finalAnswer = result.toString();
answer.setText(finalAnswer);
catch(ScriptEngineManager e){
//处理异常
System.err.println(评估脚本时出错+ e.getMessage());
}
}
如果您正在处理任何类型的异常的方法,您可以在方法签名上添加 throws
声明:
// ..其他导入
import javax.script.ScriptException;
//你的方法签名
public void actionPerformed(ActionEvent e)throws ScriptException {
// ...
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName(JavaScript);
Object result = se.eval(createEquasion);
finalAnswer = result.toString();
answer.setText(finalAnswer);
// ...
应该是这样的。 >
This is a calculator App. When I try and compile it comes up with the following message unreported exception javax.script.ScriptException; must be caught or declared to be thrown
I have a feeling it is because the class is set as ActionEvent
. I am a student so I am still learning :)
Any ideas? Thanks
\
You can do the following:
// .. other imports
import javax.script.ScriptException;
// .. the rest of your code
if(source==buteq){
try{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("JavaScript");
Object result = se.eval(createEquasion);
finalAnswer = result.toString();
answer.setText(finalAnswer);
catch(ScriptEngineManager e) {
// handle exception
System.err.println("Error evaluating the script: " + e.getMessage());
}
}
In case you are dealing with any kind of exception outside of the method you can add a throws
declaration on your methods signature:
// .. other imports
import javax.script.ScriptException;
// your method signature
public void actionPerformed (ActionEvent e) throws ScriptException {
// ...
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine se = manager.getEngineByName("JavaScript");
Object result = se.eval(createEquasion);
finalAnswer = result.toString();
answer.setText(finalAnswer);
// ...
This should be it.
这篇关于未报告的异常javax.script.ScriptException;必须被抓住或宣布被抛出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!