Throw named exceptions for IO errors

This commit is contained in:
aherbert 2021-06-09 16:31:35 +01:00
parent e300d21251
commit 6d1351cc33
2 changed files with 10 additions and 8 deletions

View File

@ -17,8 +17,9 @@
package org.apache.commons.math4.examples.sofm.chineserings;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
@ -76,9 +77,11 @@ public class StandAlone implements Callable<Void> {
*
* @param fileName File name.
* @param sofm Classifier.
* @throws UnsupportedEncodingException If UTF-8 encoding does not exist.
* @throws FileNotFoundException If the file cannot be created.
*/
private static void printResult(String fileName,
ChineseRingsClassifier sofm) {
ChineseRingsClassifier sofm) throws FileNotFoundException, UnsupportedEncodingException {
final NeuronSquareMesh2D.DataVisualization result = sofm.computeQualityIndicators();
try (final PrintWriter out = new PrintWriter(fileName, StandardCharsets.UTF_8.name())) {
@ -91,8 +94,6 @@ public class StandAlone implements Callable<Void> {
printImage("Topographic error", result.getTopographicError(), out);
printImage("Normalized hits", result.getNormalizedHits(), out);
printImage("U-matrix", result.getUMatrix(), out);
} catch (IOException e) {
// Do nothing.
}
}

View File

@ -17,8 +17,9 @@
package org.apache.commons.math4.examples.sofm.tsp;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.Callable;
@ -142,10 +143,12 @@ public class StandAlone implements Callable<Void> {
* @param fileName File.
* @param travel Solution.
* @param optimalDistance Length of shortest path.
* @throws UnsupportedEncodingException If UTF-8 encoding does not exist.
* @throws FileNotFoundException If the file cannot be created.
*/
private static void printSummary(String fileName,
City[] travel,
double optimalDistance) {
double optimalDistance) throws FileNotFoundException, UnsupportedEncodingException {
try (final PrintWriter out = new PrintWriter(fileName, StandardCharsets.UTF_8.name())) {
out.println("# Number of unique cities: " + City.unique(travel).size());
out.println("# Travel distance: " + computeDistance(travel));
@ -156,8 +159,6 @@ public class StandAlone implements Callable<Void> {
final double[] coord = c.getCoordinates();
out.println(coord[0] + " " + coord[1] + " # " + c.getName());
}
} catch (Exception e) {
// Do nothing.
}
}
}