Eclipse 自动建议未处理的异常类型异常 (Java / Weka)
Eclipse auto suggestions for Unhandled exception type Exception (Java / Weka)
我正在尝试编写一个可以在 Weka 中批处理多个 ARFF 文件的脚本。但是我总是收到 "Unhandled exception type Exception" 错误。 Eclipse 建议在每一行代码周围放置 try-catch 语句。
接受建议后代码运行。但是,如果我这样做,这是非常不可读的代码。有人知道如何解决这个未处理的异常类型异常错误吗?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;
import weka.classifiers.trees.RandomForest;
public class Eval{
public static Instances LoadARFF(String location){
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(location));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArffReader arff = null;
try {
arff = new ArffReader(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arff.getData();
}
public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");
//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;
//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);
//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);
//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
} //if
});//for each
}//Main
}//class
错误码如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type Exception
Unhandled exception type Exception
Unhandled exception type Exception
at Eval.main(Eval.java:60)
接受Eclipse自动建议后,代码大约是接受前的两倍。
我会做类似...
private static void processFile(Path filePath) {
System.out.println(filePath);
//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");
//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;
//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);
//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);
//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
}
private static void processPath(Path path) {
if(Files.isRegularFile(path)) {
try {
processFile(path);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(Eval::processPath);
}//Main
移除
throws Exception
来自您的 main
。你为什么添加那个?!?
我正在尝试编写一个可以在 Weka 中批处理多个 ARFF 文件的脚本。但是我总是收到 "Unhandled exception type Exception" 错误。 Eclipse 建议在每一行代码周围放置 try-catch 语句。
接受建议后代码运行。但是,如果我这样做,这是非常不可读的代码。有人知道如何解决这个未处理的异常类型异常错误吗?
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.functions.LinearRegression;
import weka.core.Instances;
import weka.core.converters.ArffLoader.ArffReader;
import weka.classifiers.trees.RandomForest;
public class Eval{
public static Instances LoadARFF(String location){
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(location));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArffReader arff = null;
try {
arff = new ArffReader(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return arff.getData();
}
public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(filePath -> {
if (Files.isRegularFile(filePath)) {
System.out.println(filePath);
//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");
//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;
//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);
//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);
//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
} //if
});//for each
}//Main
}//class
错误码如下:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Unhandled exception type Exception
Unhandled exception type Exception
Unhandled exception type Exception
at Eval.main(Eval.java:60)
接受Eclipse自动建议后,代码大约是接受前的两倍。
我会做类似...
private static void processFile(Path filePath) {
System.out.println(filePath);
//Get the file name of the train set.
String fileLocation = filePath.normalize().toString();
String[] tokens = fileLocation.split("[\\|/]");
String filename = tokens[tokens.length - 1];
System.out.println("Train file:" + filename + " found!");
//Get both the train and test set
String TrainFile = "path/to/sets/Train/"+ filename;
String TestFile = "path/to/sets/Test/"+ filename;
//Load the train set
Instances train = LoadARFF(TrainFile);
train.setClassIndex(train.numAttributes() - 1);
//Load the test set.
Instances test = LoadARFF(TestFile);
test.setClassIndex(train.numAttributes() - 1);
//train classifier
Classifier cls = new RandomForest();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = null;
eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(cls);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
}
private static void processPath(Path path) {
if(Files.isRegularFile(path)) {
try {
processFile(path);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
}
public static void main(String[] args) throws Exception {
//Get all files from training path
Files.walk(Paths.get("path/to/sets/Train")).forEach(Eval::processPath);
}//Main
移除
throws Exception
来自您的 main
。你为什么添加那个?!?