user2800591 user2800591 - 9 days ago 6
Java Question

error unreported exception ClassNotFoundException; must be caught or declared to be thrown

im using the snowball stemmer that can be found here http://snowball.tartarus.org/

and i was using this forum question to use a stemming algorithm for my own project

Is there a java implementation of Porter2 stemmer

i use the given class and use this code given in the previous answered post

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();


but when i come to use a try catch statement i get this error

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


really not sure how to solve this

Answer

Only classes that implements AutoCloseable can be used in try-with-resources statement declarations.

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.

Class and SnowballStemmer are not AutoCloseable. Place it inside try block instead:

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