diff --git a/ClosedXML/ClosedXML.csproj b/ClosedXML/ClosedXML.csproj index 893e0dd..0aa0a6a 100644 --- a/ClosedXML/ClosedXML.csproj +++ b/ClosedXML/ClosedXML.csproj @@ -68,6 +68,7 @@ + diff --git a/ClosedXML/Excel/CalcEngine/CalcEngine.cs b/ClosedXML/Excel/CalcEngine/CalcEngine.cs index 71e3d72..bafa237 100644 --- a/ClosedXML/Excel/CalcEngine/CalcEngine.cs +++ b/ClosedXML/Excel/CalcEngine/CalcEngine.cs @@ -816,7 +816,7 @@ private static void Throw(string msg) { - throw new Exception(msg); + throw new ExpressionParseException(msg); } #endregion ** static helpers diff --git a/ClosedXML/Excel/CalcEngine/ExpressionParseException.cs b/ClosedXML/Excel/CalcEngine/ExpressionParseException.cs new file mode 100644 index 0000000..2de2293 --- /dev/null +++ b/ClosedXML/Excel/CalcEngine/ExpressionParseException.cs @@ -0,0 +1,21 @@ +using System; +using System.Text; + +namespace ClosedXML.Excel.CalcEngine +{ + /// + /// The exception that is thrown when the strings to be parsed to an expression is invalid. + /// + public class ExpressionParseException : Exception + { + /// + /// Initializes a new instance of the ExpressionParseException class with a + /// specified error message. + /// + /// The message that describes the error. + public ExpressionParseException(string message) + : base(message) + { + } + } +} diff --git a/ClosedXML/Excel/CalcEngine/Functions/Text.cs b/ClosedXML/Excel/CalcEngine/Functions/Text.cs index cb66322..6046455 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/Text.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/Text.cs @@ -76,7 +76,7 @@ } var index = text.IndexOf(srch, start, StringComparison.Ordinal); if (index == -1) - throw new Exception("String not found."); + throw new ArgumentException("String not found."); else return index + 1; } @@ -192,7 +192,7 @@ var search = WildcardToRegex((string)p[0]); var text = (string)p[1]; - if ("" == text) throw new Exception("Invalid input string."); + if ("" == text) throw new ArgumentException("Invalid input string."); var start = 0; if (p.Count > 2) @@ -203,12 +203,12 @@ Regex r = new Regex(search, RegexOptions.Compiled | RegexOptions.IgnoreCase); var match = r.Match(text.Substring(start)); if (!match.Success) - throw new Exception("Search failed."); + throw new ArgumentException("Search failed."); else return match.Index + start + 1; //var index = text.IndexOf(search, start, StringComparison.OrdinalIgnoreCase); //if (index == -1) - // throw new Exception("String not found."); + // throw new ArgumentException("String not found."); //else // return index + 1; } @@ -233,7 +233,7 @@ int index = (int)p[3]; if (index < 1) { - throw new Exception("Invalid index in Substitute."); + throw new ArgumentException("Invalid index in Substitute."); } int pos = text.IndexOf(oldText); while (pos > -1 && index > 1) diff --git a/ClosedXML/Excel/CalcEngine/Functions/XLMatrix.cs b/ClosedXML/Excel/CalcEngine/Functions/XLMatrix.cs index 80d2663..79d6a70 100644 --- a/ClosedXML/Excel/CalcEngine/Functions/XLMatrix.cs +++ b/ClosedXML/Excel/CalcEngine/Functions/XLMatrix.cs @@ -59,7 +59,7 @@ public void MakeLU() // Function for LU decomposition { - if (!IsSquare()) throw new Exception("The matrix is not square!"); + if (!IsSquare()) throw new InvalidOperationException("The matrix is not square!"); L = IdentityMatrix(rows, cols); U = Duplicate(); @@ -79,8 +79,8 @@ k0 = i; } } - if (p == 0) - throw new Exception("The matrix is singular!"); + if (p == 0) + throw new InvalidOperationException("The matrix is singular!"); var pom1 = pi[k]; pi[k] = pi[k0]; @@ -115,8 +115,8 @@ public XLMatrix SolveWith(XLMatrix v) // Function solves Ax = v in confirmity with solution vector "v" { - if (rows != cols) throw new Exception("The matrix is not square!"); - if (rows != v.rows) throw new Exception("Wrong number of results in solution vector!"); + if (rows != cols) throw new InvalidOperationException("The matrix is not square!"); + if (rows != v.rows) throw new ArgumentException("Wrong number of results in solution vector!"); if (L == null) MakeLU(); var b = new XLMatrix(rows, 1); @@ -242,9 +242,9 @@ for (var j = 0; j < nums.Length; j++) matrix[i, j] = double.Parse(nums[j]); } } - catch (FormatException) + catch (FormatException fe) { - throw new Exception("Wrong input format!"); + throw new FormatException("Wrong input format!", fe); } return matrix; } @@ -345,7 +345,7 @@ private static XLMatrix StrassenMultiply(XLMatrix A, XLMatrix B) // Smart matrix multiplication { - if (A.cols != B.rows) throw new Exception("Wrong dimension of matrix!"); + if (A.cols != B.rows) throw new ArgumentException("Wrong dimension of matrix!"); XLMatrix R; @@ -513,7 +513,7 @@ public static XLMatrix StupidMultiply(XLMatrix m1, XLMatrix m2) // Stupid matrix multiplication { - if (m1.cols != m2.rows) throw new Exception("Wrong dimensions of matrix!"); + if (m1.cols != m2.rows) throw new ArgumentException("Wrong dimensions of matrix!"); var result = ZeroMatrix(m1.rows, m2.cols); for (var i = 0; i < result.rows; i++) @@ -535,7 +535,7 @@ private static XLMatrix Add(XLMatrix m1, XLMatrix m2) { if (m1.rows != m2.rows || m1.cols != m2.cols) - throw new Exception("Matrices must have the same dimensions!"); + throw new ArgumentException("Matrices must have the same dimensions!"); var r = new XLMatrix(m1.rows, m1.cols); for (var i = 0; i < r.rows; i++) for (var j = 0; j < r.cols; j++) diff --git a/ClosedXML/Excel/CalcEngine/XLCalcEngine.cs b/ClosedXML/Excel/CalcEngine/XLCalcEngine.cs index 6d5dc53..108ea40 100644 --- a/ClosedXML/Excel/CalcEngine/XLCalcEngine.cs +++ b/ClosedXML/Excel/CalcEngine/XLCalcEngine.cs @@ -92,7 +92,7 @@ { if (_evaluating) { - throw new Exception("Circular Reference"); + throw new InvalidOperationException("Circular Reference"); } try { diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index fc5db80..39a4928 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -281,7 +281,7 @@ if (TryGetValue(out retVal)) return retVal; - throw new Exception("Cannot convert cell value to " + typeof(T)); + throw new FormatException("Cannot convert cell value to " + typeof(T)); } public string GetString() diff --git a/ClosedXML/Excel/Ranges/XLRangeAddress.cs b/ClosedXML/Excel/Ranges/XLRangeAddress.cs index df631ec..fbe5135 100644 --- a/ClosedXML/Excel/Ranges/XLRangeAddress.cs +++ b/ClosedXML/Excel/Ranges/XLRangeAddress.cs @@ -84,7 +84,7 @@ get { if (IsInvalid) - throw new Exception("Range is invalid."); + throw new InvalidOperationException("Range is invalid."); return _firstAddress; } @@ -96,7 +96,7 @@ get { if (IsInvalid) - throw new Exception("Range is an invalid state."); + throw new InvalidOperationException("Range is an invalid state."); return _lastAddress; } diff --git a/ClosedXML/Excel/Style/Colors/XLColor_Public.cs b/ClosedXML/Excel/Style/Colors/XLColor_Public.cs index 6feefe3..4d0d9d3 100644 --- a/ClosedXML/Excel/Style/Colors/XLColor_Public.cs +++ b/ClosedXML/Excel/Style/Colors/XLColor_Public.cs @@ -55,7 +55,7 @@ get { if (_colorType == XLColorType.Theme) - throw new Exception("Cannot convert theme color to Color."); + throw new InvalidOperationException("Cannot convert theme color to Color."); if (_colorType == XLColorType.Indexed) if (_indexed == TOOLTIPCOLORINDEX) @@ -72,12 +72,12 @@ get { if (ColorType == XLColorType.Theme) - throw new Exception("Cannot convert theme color to indexed color."); + throw new InvalidOperationException("Cannot convert theme color to indexed color."); if (ColorType == XLColorType.Indexed) return _indexed; - throw new Exception("Cannot convert Color to indexed color."); + throw new InvalidOperationException("Cannot convert Color to indexed color."); } } @@ -89,9 +89,9 @@ return _themeColor; if (ColorType == XLColorType.Indexed) - throw new Exception("Cannot convert indexed color to theme color."); + throw new InvalidOperationException("Cannot convert indexed color to theme color."); - throw new Exception("Cannot convert Color to theme color."); + throw new InvalidOperationException("Cannot convert Color to theme color."); } } @@ -103,7 +103,7 @@ return _themeTint; if (ColorType == XLColorType.Indexed) - throw new Exception("Cannot extract theme tint from an indexed color."); + throw new InvalidOperationException("Cannot extract theme tint from an indexed color."); return _color.A/255.0; } diff --git a/ClosedXML/Excel/XLWorkbook.cs b/ClosedXML/Excel/XLWorkbook.cs index 9e86a67..a70e74a 100644 --- a/ClosedXML/Excel/XLWorkbook.cs +++ b/ClosedXML/Excel/XLWorkbook.cs @@ -442,7 +442,7 @@ { checkForWorksheetsPresent(); if (_loadSource == XLLoadSource.New) - throw new Exception("This is a new file, please use one of the SaveAs methods."); + throw new InvalidOperationException("This is a new file, please use one of the SaveAs methods."); if (_loadSource == XLLoadSource.Stream) { @@ -502,7 +502,8 @@ private static SpreadsheetDocumentType GetSpreadsheetDocumentType(string filePath) { var extension = Path.GetExtension(filePath); - if (extension == null) throw new Exception("Empty extension is not supported."); + + if (extension == null) throw new ArgumentException("Empty extension is not supported."); extension = extension.Substring(1).ToLowerInvariant(); switch (extension) @@ -522,7 +523,7 @@ private void checkForWorksheetsPresent() { if (Worksheets.Count() == 0) - throw new Exception("Workbooks need at least one worksheet."); + throw new InvalidOperationException("Workbooks need at least one worksheet."); } /// diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 2389524..14a1db1 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -725,7 +725,7 @@ if (shape != null) break; } - if (xdoc == null) throw new Exception("Could not load comments file"); + if (xdoc == null) throw new ArgumentException("Could not load comments file"); return xdoc; } diff --git a/ClosedXML/Excel/XLWorksheets.cs b/ClosedXML/Excel/XLWorksheets.cs index c7b15d4..e3f42b7 100644 --- a/ClosedXML/Excel/XLWorksheets.cs +++ b/ClosedXML/Excel/XLWorksheets.cs @@ -77,18 +77,18 @@ if (wss.Any()) return wss.First().Value; - throw new Exception("There isn't a worksheet named '" + sheetName + "'."); + throw new ArgumentException("There isn't a worksheet named '" + sheetName + "'."); } public IXLWorksheet Worksheet(Int32 position) { int wsCount = _worksheets.Values.Count(w => w.Position == position); if (wsCount == 0) - throw new Exception("There isn't a worksheet associated with that position."); + throw new ArgumentException("There isn't a worksheet associated with that position."); if (wsCount > 1) { - throw new Exception( + throw new ArgumentException( "Can't retrieve a worksheet because there are multiple worksheets associated with that position."); } @@ -130,10 +130,10 @@ { int wsCount = _worksheets.Values.Count(w => w.Position == position); if (wsCount == 0) - throw new Exception("There isn't a worksheet associated with that index."); + throw new ArgumentException("There isn't a worksheet associated with that index."); if (wsCount > 1) - throw new Exception( + throw new ArgumentException( "Can't delete the worksheet because there are multiple worksheets associated with that index."); var ws = _worksheets.Values.Single(w => w.Position == position); diff --git a/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs b/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs index 3fca48e..3ad7f8e 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/FunctionsTests.cs @@ -620,7 +620,7 @@ Assert.AreEqual(0, cell.Value); cell = wb.Worksheet(1).Cell(3, 1).SetFormulaA1("=SUM(D1,D2)"); Assert.AreEqual(0, cell.Value); - Assert.That(() => wb.Worksheet(1).Cell(3, 1).SetFormulaA1("=AVERAGE(D1,D2)").Value, Throws.Exception); + Assert.That(() => wb.Worksheet(1).Cell(3, 1).SetFormulaA1("=AVERAGE(D1,D2)").Value, Throws.TypeOf()); } [Test] diff --git a/ClosedXML_Tests/Excel/CalcEngine/LookupTests.cs b/ClosedXML_Tests/Excel/CalcEngine/LookupTests.cs index a112f3d..60427e6 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/LookupTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/LookupTests.cs @@ -116,11 +116,11 @@ [Test] public void Vlookup_Exceptions() { - Assert.That(() => workbook.Evaluate(@"=VLOOKUP("""",Data!$B$2:$I$71,3,FALSE)"), Throws.Exception); - Assert.That(() => workbook.Evaluate(@"=VLOOKUP(50,Data!$B$2:$I$71,3,FALSE)"), Throws.Exception); - Assert.That(() => workbook.Evaluate(@"=VLOOKUP(20,Data!$B$2:$I$71,9,FALSE)"), Throws.Exception); + Assert.That(() => workbook.Evaluate(@"=VLOOKUP("""",Data!$B$2:$I$71,3,FALSE)"), Throws.TypeOf()); + Assert.That(() => workbook.Evaluate(@"=VLOOKUP(50,Data!$B$2:$I$71,3,FALSE)"), Throws.TypeOf()); + Assert.That(() => workbook.Evaluate(@"=VLOOKUP(20,Data!$B$2:$I$71,9,FALSE)"), Throws.TypeOf()); - Assert.That(() => workbook.Evaluate(@"=VLOOKUP(-1,Data!$B$2:$I$71,9,TRUE)"), Throws.Exception); + Assert.That(() => workbook.Evaluate(@"=VLOOKUP(-1,Data!$B$2:$I$71,9,TRUE)"), Throws.TypeOf()); } } } diff --git a/ClosedXML_Tests/Excel/CalcEngine/StatisticalTests.cs b/ClosedXML_Tests/Excel/CalcEngine/StatisticalTests.cs index c59e34d..2550868 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/StatisticalTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/StatisticalTests.cs @@ -22,7 +22,7 @@ value = ws.Evaluate("AVERAGE(G3:G45)").CastTo(); Assert.AreEqual(49.3255814, value, tolerance); - Assert.That(() => ws.Evaluate("AVERAGE(D3:D45)"), Throws.Exception); + Assert.That(() => ws.Evaluate("AVERAGE(D3:D45)"), Throws.TypeOf()); } [Test] @@ -146,7 +146,7 @@ { var ws = workbook.Worksheets.First(); double value; - Assert.That(() => ws.Evaluate(@"=STDEV(D3:D45)"), Throws.Exception); + Assert.That(() => ws.Evaluate(@"=STDEV(D3:D45)"), Throws.TypeOf()); value = ws.Evaluate(@"=STDEV(H3:H45)").CastTo(); Assert.AreEqual(47.34511769, value, tolerance); @@ -163,7 +163,7 @@ { var ws = workbook.Worksheets.First(); double value; - Assert.That(() => ws.Evaluate(@"=STDEVP(D3:D45)"), Throws.Exception); + Assert.That(() => ws.Evaluate(@"=STDEVP(D3:D45)"), Throws.InvalidOperationException); value = ws.Evaluate(@"=STDEVP(H3:H45)").CastTo(); Assert.AreEqual(46.79135458, value, tolerance); @@ -180,7 +180,7 @@ { var ws = workbook.Worksheets.First(); double value; - Assert.That(() => ws.Evaluate(@"=VAR(D3:D45)"), Throws.Exception); + Assert.That(() => ws.Evaluate(@"=VAR(D3:D45)"), Throws.InvalidOperationException); value = ws.Evaluate(@"=VAR(H3:H45)").CastTo(); Assert.AreEqual(2241.560169, value, tolerance); @@ -197,7 +197,7 @@ { var ws = workbook.Worksheets.First(); double value; - Assert.That(() => ws.Evaluate(@"=VARP(D3:D45)"), Throws.Exception); + Assert.That(() => ws.Evaluate(@"=VARP(D3:D45)"), Throws.InvalidOperationException); value = ws.Evaluate(@"=VARP(H3:H45)").CastTo(); Assert.AreEqual(2189.430863, value, tolerance); diff --git a/ClosedXML_Tests/Excel/CalcEngine/TextTests.cs b/ClosedXML_Tests/Excel/CalcEngine/TextTests.cs index 3a89de2..efb58d3 100644 --- a/ClosedXML_Tests/Excel/CalcEngine/TextTests.cs +++ b/ClosedXML_Tests/Excel/CalcEngine/TextTests.cs @@ -1,4 +1,5 @@ using ClosedXML.Excel; +using ClosedXML.Excel.CalcEngine; using NUnit.Framework; using System; using System.Globalization; @@ -19,13 +20,13 @@ [Test] public void Char_Empty_Input_String() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Char("""")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Char("""")"), Throws.TypeOf()); } [Test] public void Char_Input_Too_Large() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Char(9797)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Char(9797)"), Throws.TypeOf()); } [Test] @@ -56,7 +57,7 @@ public void Code_Empty_Input_String() { // Todo: more specific exception - ValueException? - Assert.That(() => XLWorkbook.EvaluateExpr(@"Code("""")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Code("""")"), Throws.TypeOf()); } [Test] @@ -82,7 +83,7 @@ [Test] public void Dollar_Empty_Input_String() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Dollar("", 3)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Dollar("", 3)"), Throws.TypeOf()); } [Test] @@ -121,26 +122,26 @@ [Test] public void Find_Start_Position_Too_Large() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""abc"", ""abcdef"", 10)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""abc"", ""abcdef"", 10)"), Throws.TypeOf()); } [Test] public void Find_String_In_Another_Empty_String() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""abc"", """")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""abc"", """")"), Throws.TypeOf()); } [Test] public void Find_String_Not_Found() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""123"", ""asdf"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""123"", ""asdf"")"), Throws.TypeOf()); } [Test] public void Find_Case_Sensitive_String_Not_Found() { // Find is case-sensitive - Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""excel"", ""Microsoft Excel 2010"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Find(""excel"", ""Microsoft Excel 2010"")"), Throws.TypeOf()); } [Test] @@ -159,7 +160,7 @@ [Test] public void Fixed_Input_Is_String() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Fixed(""asdf"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Fixed(""asdf"")"), Throws.TypeOf()); } [Test] @@ -297,7 +298,7 @@ [Test] public void Rept_Start_Is_Negative() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Rept(""Francois"", -1)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Rept(""Francois"", -1)"), Throws.TypeOf()); } [Test] @@ -344,7 +345,7 @@ [Test] public void Search_No_Parameters_With_Values() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search("""", """")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search("""", """")"), Throws.TypeOf()); } [Test] @@ -357,31 +358,31 @@ [Test] public void Search_Start_Position_Too_Large() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""abc"", ""abcdef"", 10)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""abc"", ""abcdef"", 10)"), Throws.TypeOf()); } [Test] public void Search_Empty_Input_String() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""abc"", """")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""abc"", """")"), Throws.TypeOf()); } [Test] public void Search_String_Not_Found() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""123"", ""asdf"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""123"", ""asdf"")"), Throws.TypeOf()); } [Test] public void Search_Wildcard_String_Not_Found() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""soft?2010"", ""Microsoft Excel 2010"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""soft?2010"", ""Microsoft Excel 2010"")"), Throws.TypeOf()); } [Test] public void Search_Start_Position_Too_Large2() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""text"", ""This is some text"", 15)"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Search(""text"", ""This is some text"", 15)"), Throws.TypeOf()); } // http://www.excel-easy.com/examples/find-vs-search.html @@ -517,7 +518,7 @@ [Test] public void Value_Input_String_Is_Not_A_Number() { - Assert.That(() => XLWorkbook.EvaluateExpr(@"Value(""asdf"")"), Throws.Exception); + Assert.That(() => XLWorkbook.EvaluateExpr(@"Value(""asdf"")"), Throws.TypeOf()); } [Test] diff --git a/ClosedXML_Tests/packages.config b/ClosedXML_Tests/packages.config index c32aa84..5e0ab7c 100644 --- a/ClosedXML_Tests/packages.config +++ b/ClosedXML_Tests/packages.config @@ -1,5 +1,5 @@ - - - - + + + + \ No newline at end of file