5

I read a lot of pages about the try/catch/finally process in java, but I didn't succeed to catch the ClassNotFoundException in my code. I used a for loop to open and save in rdfxml format every file in a directory I give as the argument. What I want is when I'm performing the code I get the e.getmessage() and the loop continue to treat the next file.

The problem is my script works perfectly fine but I don't receive the e.getmessage() when the ClassNotFoundException occurs.

@SuppressWarnings("finally")
public static void main(String[] args) {

    File dir = new File(args[0]);
    File[] directoryListing = dir.listFiles();

    if (directoryListing != null) {
        for (File owlFile : directoryListing){
            String name = owlFile.getName();
                    try {
                        translateOwlToRdfXml(owlFile,name);
                    }
                    catch (OWLOntologyCreationException|OWLOntologyStorageException|ClassNotFoundException e) {
                        System.out.println(name + " has not been saved into RDF/XML;");
                        System.out.println(e.getMessage());
                        }
                    finally {
                        continue;
                    }
                }

            }
        }


    static void translateOwlToRdfXml(File owlFile, String name) throws OWLOntologyCreationException, OWLOntologyStorageException, ClassNotFoundException {

            OWLOntologyManager manager = OWLManager.createOWLOntologyManager();

        System.out.println("Loaded ontology: " + name);
        OWLOntology ontology;
        ontology = manager.loadOntologyFromOntologyDocument(owlFile);
        RDFXMLOntologyFormat rdfxmlFormat = new RDFXMLOntologyFormat();

            manager.saveOntology(ontology, rdfxmlFormat, IRI.create(owlFile));
        System.out.println("Saved ontology in RDF/OWL: " + name);

    }
}

When I don't write the "finally" block, I receive this message:

Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: java.lang.NoClassDefFoundError: com/github/jsonldjava/core/JsonLdError
    at org.openrdf.rio.jsonld.JSONLDParserFactory.getParser(JSONLDParserFactory.java:37)
    at org.openrdf.rio.Rio.createParser(Rio.java:195)
    at org.semanticweb.owlapi.rio.RioParserImpl.parseDocumentSource(RioParserImpl.java:241)
    at org.semanticweb.owlapi.rio.RioParserImpl.parse(RioParserImpl.java:191)
    at uk.ac.manchester.cs.owl.owlapi.ParsableOWLOntologyFactory.loadOWLOntology(ParsableOWLOntologyFactory.java:161)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:975)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOntologyDocument(OWLOntologyManagerImpl.java:913)
    at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOntologyDocument(OWLOntologyManagerImpl.java:928)
    at owl2rdf.owl2rdf.translateOwlToRdfXml(owl2rdf.java:57)
    at owl2rdf.owl2rdf.main(owl2rdf.java:32)
    ... 5 more
Caused by: java.lang.ClassNotFoundException: com.github.jsonldjava.core.JsonLdError
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 15 more

But when I write the "finally", I receive nothing and the code continue to treat the next file. I thought I don't understand clearly how try/catch and throw methods work.

Thanks in advance

Sweeper
  • 213,210
  • 22
  • 193
  • 313
Cyril
  • 485
  • 4
  • 15
  • it might be because of `continue;` statement. – Rustam Aug 13 '15 at 09:53
  • 2
    It throws java.lang.reflect.InvocationTargetException, not any of the Exception you catch(), thus the exception wasn't really caught. The finally() and continue; make the loop to continue. – Ken Cheung Aug 13 '15 at 09:55
  • As Ken said the ClassNotFoundException is wrapped in an InvocationTargetException and thus won't be caught by your catch block. Continuing in the finally block messes with the control flow and thus it continues with the loop but doing so is not good style since the finally block will _always_ be executed, even if there is no exception, and this might lead to bugs in the future. – Thomas Aug 13 '15 at 09:56
  • Btw: The `continue` in you `finally` block does not make sense (actually, the whole `finally` can be removed in your case. After the try-catch there is no statement but the loop starts again. Thus, your finally-continue code is the same as putting a continue as a last statement of a loop. Thus, it's completely unnecessary. – Matthias J. Sax Aug 13 '15 at 10:07

3 Answers3

3

You only catch exceptions you list in catch(...). The stack trace shows the exception in the main method is an NoClassDefFoundError.

To solve it replace

catch (OWLOntologyCreationException|OWLOntologyStorageException|ClassNotFoundException e) {
      System.out.println(name + " has not been saved into RDF/XML;");
      System.out.println(e.getMessage());
}

By (here you catch all exceptions)

catch (Exception e) {
         System.out.println(name + " has not been saved into RDF/XML;");
         System.out.println(e.getMessage());
}

Or by

catch (OWLOntologyCreationException|OWLOntologyStorageException
|ClassNotFoundException|NoClassDefFoundErrore) {
             System.out.println(name + " has not been saved into RDF/XML;");
             System.out.println(e.getMessage());
}
Erwan C.
  • 709
  • 5
  • 18
  • `translateOwlToRdfXml` throws `NoClassDefFoundError`. InvocationTargetException is thrown on a higher level. – pkalinow Aug 13 '15 at 10:03
  • Thanks for your replies. As you imagine I'm a new in Java. Exactly, it's the `NoClassDefFoundError` I must to throw and catch. – Cyril Aug 13 '15 at 10:12
1

Try catching NoClassDefFoundError instead of ClassNotFoundException. The latter is wrapped by the former.

See the following stack trace fragment - it means that the method throws NoClassDefFoundError:

Caused by: java.lang.NoClassDefFoundError: com/github/jsonldjava/core/JsonLdError
at org.openrdf.rio.jsonld.JSONLDParserFactory.getParser(JSONLDParserFactory.java:37)
at org.openrdf.rio.Rio.createParser(Rio.java:195)
at org.semanticweb.owlapi.rio.RioParserImpl.parseDocumentSource(RioParserImpl.java:241)
at org.semanticweb.owlapi.rio.RioParserImpl.parse(RioParserImpl.java:191)
at uk.ac.manchester.cs.owl.owlapi.ParsableOWLOntologyFactory.loadOWLOntology(ParsableOWLOntologyFactory.java:161)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntology(OWLOntologyManagerImpl.java:975)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOntologyDocument(OWLOntologyManagerImpl.java:913)
at uk.ac.manchester.cs.owl.owlapi.OWLOntologyManagerImpl.loadOntologyFromOntologyDocument(OWLOntologyManagerImpl.java:928)
at owl2rdf.owl2rdf.translateOwlToRdfXml(owl2rdf.java:57)
pkalinow
  • 1,619
  • 1
  • 17
  • 43
0

translateOwlToRdfXml throwing InvocationTargetException.

InvocationTargetException is a checked exception that wraps
an exception thrown by an invoked method or constructor.

You need to catch this exception and then use getTargetException to get the NoClassDefFoundError. From there you can get the getCause gives you the wrapper ClassNotFoundException.

Dakshinamurthy Karra
  • 5,353
  • 1
  • 17
  • 28