Unreported exception javax.script.ScriptException; must be caught or declared to be thrown

痴心易碎 提交于 2020-01-16 18:41:12

问题


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

\


回答1:


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.




回答2:


In your this actionPerformed method the line se.eval(createEquasion); is throwing ScriptException. you have to handle this Excpetion.

Add below code to complie the class

Object result = null;
            try {
                result = se.eval(createEquasion);
            } catch (ScriptException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (result != null)
                finalAnswer = result.toString();



回答3:


ScriptEngine.html#eval can throws two Exception

  1. NullPointerException
  2. ScriptException

NullPointerException is RuntimeException so compiler will not force you to handle this.But ScriptException is checked Exception, so compiler will force you to handle this Exception either insert the code block into try catch or throw it.

try-catch block

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(ScriptException se){
            ...
       }
}

ScriptException class declaration -

public class ScriptException extends Exception{...}

Please find more information -

  • Checked Exception ScriptException
  • ScriptEngine.html#eval(java.lang.String)


来源:https://stackoverflow.com/questions/15195253/unreported-exception-javax-script-scriptexception-must-be-caught-or-declared-to

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!