2016-06-08 71 views
0

我想寫一個腳本,可以在Weka中批處理多個ARFF文件。但是我總是得到「未處理的異常類型異常」錯誤。 Eclipse建議在每行代碼中放置try-catch語句。未處理的異常類型的Eclipse自動建議異常(Java/Weka)

接受代碼運行的建議後。但是,如果我這樣做,這是非常不可接受的代碼。任何機構的一個想法如何解決這個未處理的異常類型異常錯誤?

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的自動建議後,代碼爲約兩倍大的接受了。

回答

1

我會做這樣的事情......

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 
0

main刪除

throws Exception 

。你爲什麼添加?!?

+0

否則'Files.walk'不起作用。我是Java的新手,所以我接受了Eclipse的建議,將拋出的Exception添加到主類中。 – atMaarten

+0

正確處理該異常*。出現IO錯誤,然後您想要打印錯誤消息。 **不要使用你不明白其後果的日食建議。**它們被認爲是很常見的錯誤(例如,對日食的靜態建議幾乎總是錯誤的做法並導致新的錯誤回報) –

+0

主要爲此應用程序提供'throws Exception'有什麼危害?它看起來像一個非常簡單的示例應用程序。我猜測,如果發生異常,將堆棧跟蹤轉儲到控制檯並退出(當main拋出異常時會發生什麼)是完全可以接受的。此外,主要方法中的「拋出異常」並不是真正導致或與所述投訴有關。這不僅僅是一個回答而是一個評論。 – Pace