diff --git a/src/SparseTransform.CLI/Program.cs b/src/SparseTransform.CLI/Program.cs index 952047c6309416c0e45e13597d8d782b4594fb63..416dbe67d3497099f22c4335b7f428115c196728 100644 --- a/src/SparseTransform.CLI/Program.cs +++ b/src/SparseTransform.CLI/Program.cs @@ -10,31 +10,46 @@ public class Program Parser.Default.ParseArguments<ConvertOptions, PartitionOptions>(args) .WithParsed<ConvertOptions>(o => { - - String MMMatrix = File.ReadAllText(o.Input); - String DotString = Convert2Dot(MMMatrix); - File.WriteAllText(o.Output, DotString); + try + { + String MMMatrix = File.ReadAllText(o.Input); + String DotString = Convert2Dot(MMMatrix); + File.WriteAllText(o.Output, DotString); + } + catch (Exception e) + { + Console.Out.WriteLine(e.Message); + return; + } }) .WithParsed<PartitionOptions>(o => { - String MMMatrix = File.ReadAllText(o.Input); - String Output = ""; - - if (o.Format == Formats.ColoredDot) + try { - Output = Partition2ColoredDot(MMMatrix, o.TextColor); - } - else if (o.Format == Formats.SeedMatrix) - { - Output = Partition2SeedMatrix(MMMatrix); + String MMMatrix = File.ReadAllText(o.Input); + String Output = ""; + + if (o.Format == Formats.ColoredDot) + { + Output = Partition2ColoredDot(MMMatrix, o.TextColor); + } + else if (o.Format == Formats.SeedMatrix) + { + Output = Partition2SeedMatrix(MMMatrix); + } + else if (o.Format == Formats.CompressedMatrix) + { + Output = Partition2CompressedMatrix(MMMatrix); + } + + File.WriteAllText(o.Output, Output); } - else if (o.Format == Formats.CompressedMatrix) + catch (Exception e) { - Output = Partition2CompressedMatrix(MMMatrix); + Console.Out.WriteLine(e.Message); + return; } - - File.WriteAllText(o.Output, Output); }); } diff --git a/src/SparseTransform/Convert/ErrorListener.cs b/src/SparseTransform/Convert/ErrorListener.cs index 657aacc982795ca1fa28396317f49e4c579759df..4fd6daf279e10030ec2edc4f82e752d16eb70bfa 100644 --- a/src/SparseTransform/Convert/ErrorListener.cs +++ b/src/SparseTransform/Convert/ErrorListener.cs @@ -22,7 +22,7 @@ namespace Transform.Convert /// <exception cref="Exception"></exception> public void SyntaxError([NotNull] IRecognizer recognizer, [Nullable] IToken offendingSymbol, int line, int charPositionInLine, [NotNull] string msg, [Nullable] RecognitionException e) { - throw new Exception(msg); + throw new Exception("Error in MatrixMarket header line: " + msg); } } } diff --git a/src/SparseTransform/Convert/MatrixMarketReader.cs b/src/SparseTransform/Convert/MatrixMarketReader.cs index 27a114d81ea5966f20dd205e24fa4e41488968d5..ee16ecc747897526087cbdf4444c0597a8657af7 100644 --- a/src/SparseTransform/Convert/MatrixMarketReader.cs +++ b/src/SparseTransform/Convert/MatrixMarketReader.cs @@ -86,7 +86,7 @@ namespace SparseTransform.Convert /// checks whether the comment-section is valid. /// </summary> /// <param name="lines">entire input split by newlines</param> - /// <exception cref="Exception"></exception> + /// <exception cref="FormatException"></exception> private void checkComments(string[] lines) { bool leftComments = false; @@ -96,7 +96,7 @@ namespace SparseTransform.Convert { if (leftComments && line.StartsWith('%')) { - throw new Exception("comment section mismatch on input"); + throw new FormatException("Syntax Error in MatrixMarket file: Encountered more comments in between data lines."); } else { @@ -126,31 +126,24 @@ namespace SparseTransform.Convert string[] values; int lineCount = getFirstDataLine(matrix); matrix[lineCount] = "% " + matrix[lineCount]; - try + foreach (String line in matrix) { - foreach (String line in matrix) + if (line != "") { - if (line != "") + if (!line.StartsWith('%')) { - if (!line.StartsWith('%')) - { - values = line.Split(" "); - if (values.Length != 3) - { - throw new Exception(); - } - int xCoordinate = Int32.Parse(values[0]); - int yCoordinate = Int32.Parse(values[1]); - processor(xCoordinate, yCoordinate, Double.Parse(values[2])); + values = line.Split(" "); + if (values.Length != 3) + { + throw new ArgumentException("Syntax Error in MatrixMarket file: Data line did not contain three elements."); } + int xCoordinate = Int32.Parse(values[0]); + int yCoordinate = Int32.Parse(values[1]); + processor(xCoordinate, yCoordinate, Double.Parse(values[2])); } } } - catch (Exception e) - { - throw e; - } }