java - 错误未报告异常 ClassNotFoundException;必须被捕获或宣布被扔出

标签 java error-handling porter-stemmer

我正在使用雪球词干分析器,可以在此处找到 http://snowball.tartarus.org/

我正在使用这个论坛问题来为我自己的项目使用词干算法

Is there a java implementation of Porter2 stemmer

我使用给定的类并使用之前回答的帖子中给出的代码

Class stemClass = Class.forName("org.tartarus.snowball.ext." + lang + "Stemmer");
stemmer = (SnowballProgram) stemClass.newInstance();
stemmer.setCurrent("your_word");
stemmer.stem();
String your_stemmed_word = stemmer.getCurrent();  

但是当我使用 try catch 语句时,我收到此错误

assmt1/invert3.java:339: error: incompatible types: try-with-resources not applicable to variable type
      try(  Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
                  ^
(Class cannot be converted to AutoCloseable)
assmt1/invert3.java:340: error: incompatible types: try-with-resources not applicable to variable type
        SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance())
                        ^
(SnowballStemmer cannot be converted to AutoCloseable)
2 errors

真的不知道如何解决这个问题

最佳答案

只有实现了AutoCloseable的类才能在 try-with-resources 中使用声明声明。

The try-with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try-with-resources statement ensures that each resource is closed at the end of the statement. Any object that implements java.lang.AutoCloseable, which includes all objects which implement java.io.Closeable, can be used as a resource.

ClassSnowballStemmer 不是 AutoCloseable。将其放在 try block 内:

try {
    Class stemClass = Class.forName("org.tartarus.snowball.ext." +"english"+ "Stemmer");
    SnowballStemmer stemmer = (SnowballStemmer) stemClass.newInstance();
} catch(Exception e){
    //Do Something
} finally {
    //Do Something
}

关于java - 错误未报告异常 ClassNotFoundException;必须被捕获或宣布被扔出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40797827/

相关文章:

c# - 引发异常时不显示自定义错误页面

java - 我可以访问为线程池线程提交任务(并且正在运行)的线程吗?

asp.net - 将querystring传递到404页面

php - 我可以在将 html 文件加载到 DOMDocument 时 try catch PHP 警告吗?

python - 将 porters stemmer 应用于每个单词的 Pandas 列

python - PorterStemmer 似乎不起作用

php - 产生真实单词的词干提取算法

java - 空指针异常 Calendar.setTime

java - 如何从 Android 手机读取 Java 中的 CPU "stats"?

java - 根据标识符读取文本文件并创建另一组文本文件