java - weka java api stringtovector 异常

标签 java api exception machine-learning weka

所以我有这个使用 Weka 的 Java API 的代码:

  String html = "blaaah";
    Attribute input = new Attribute("html",(FastVector) null);

    FastVector inputVec = new FastVector();
    inputVec.addElement(input);

    Instances htmlInst = new Instances("html",inputVec,1);
    htmlInst.add(new Instance(1));  
    htmlInst.instance(0).setValue(0, html);

    System.out.println(htmlInst);

StringToWordVector filter = new StringToWordVector();
filter.setInputFormat(htmlInst);
Instances dataFiltered = Filter.useFilter(htmlInst, filter);

但是在filter.setInputFormat(htmlInst)行上,Java提示该函数抛出了未处理的异常...

我做错了什么?

最佳答案

当函数显式抛出异常时,必须发生以下两种情况之一

  1. 调用函数必须在 try-catch block 中处理异常
  2. 调用函数必须向其调用函数抛出异常(因此您必须选择实际使用 try-catch block 来处理异常的某个点)

根据此处的文档:http://www.lri.fr/~pierres/donn%E9es/save/these/weka-3-4/doc/weka/filters/unsupervised/attribute/StringToWordVector.html#setInputFormat(weka.core.Instances )这个函数抛出一个普通的旧Exception。不是 super 描述性的,但仍然需要适当处理。

你可以这样做:

try {
    StringToWordVector filter = new StringToWordVector();
    filter.setInputFormat(htmlInst);
    Instances dataFiltered = Filter.useFilter(htmlInst, filter);
} catch (Exception e) {
    System.err.println("Exception caught during formatting: " + e.getMessage());
    return;
}

如果您希望让另一个函数处理异常,请更改方法签名以显式引发异常:

private Object formatMyString(String s) throws Exception {
    ...
}

关于java - weka java api stringtovector 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6644191/

相关文章:

java - 在gradle中为Heroku创建webapp-runner.jar

javascript - 谷歌浏览器也有一个控制台对象。它的等效 API 页面在哪里?

javascript - Google Maps API v3 - 在上周左右发生了变化?

c++ - 在编译时检测异常是否被禁用

java - 抛出未处理的异常后线程卡住

java - Solr 不区分大小写的查询

java - 为什么 StreamEx 在收集到列表时强制我将 "? extends"添加到变量类型?

java - 发出实例化 bean - 验证错误

api - 使用 Flutter 向 Flask API 发送 http Post-Request(包含图像)

java - 为什么我得到 "Unhandled exception type IOException"