gpt4 book ai didi

java - ClassNotFoundException 我没有成功捕获

转载 作者:行者123 更新时间:2023-11-30 03:18:18 24 4
gpt4 key购买 nike

我读了很多关于java中的try/catch/finally过程的页面,但是我没有成功在我的代码中捕获ClassNotFoundException。我使用 for 循环 打开并以 rdfxml 格式保存我作为参数给出的目录中的每个文件。我想要的是,当我执行代码时,我得到 e.getmessage() 并且循环继续处理下一个文件。

问题是我的脚本工作得很好,但当发生 ClassNotFoundException 时我没有收到 e.getmessage() 。

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

}
}

当我不写“finally” block 时,我会收到以下消息:

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

但是当我写下“最后”时,我什么也没收到,代码继续处理下一个文件。我想我不太明白 try/catch 和 throw 方法是如何工作的。

提前致谢

最佳答案

您只能捕获在 catch(...) 中列出的异常。堆栈跟踪显示 main 方法中的异常是 NoClassDefFoundError。

要解决这个问题,请替换

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

通过(在这里你可以捕获所有异常)

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

或者通过

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

关于java - ClassNotFoundException 我没有成功捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31984861/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com