How to properly handle exception?

  • Thread starter Thread starter cataster
  • Start date Start date
C

cataster

Guest
I am scanning a JSON file and running the fieldValue() function that dynamically extracts the values from the json "fields". Particularly, since the JSON input is not a typical property-value (key/value) JSON structure, i am skipping over the field name line and returning the .text property of the next line.


For example: If lines[3].text is "Owner Full Name", then lines[3+1] is "Bob Lee"


As part of exception handling, I realize the the "odd-even" structure may not be present in some edge cases, like the sample json file below. So thats why i explicitly defined the exception handling, so i expect this exception to be thrown "Odd number of items found in IEnumerable<>" yet a completely different exception is being thrown! why is that? the exception is occurring on this line: return skipped.Skip(1).First().text;


Exception:

1) Exception Information
*********************************************
Exception Type: System.InvalidOperationException
TargetSite: Void ThrowNoElementsException()
Message: Sequence contains no elements
Data: System.Collections.ListDictionaryInternal
HelpLink: NULL
Source: System.Linq
HResult: -2146233079

StackTrace Information
*********************************************
at System.Linq.ThrowHelper.ThrowNoElementsException()
at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
at Program.fieldValue(IEnumerable`1 lines, String nameOfField, Boolean throwException)


Code:

public static string fieldValue(IEnumerable<dynamic> lines, string nameOfField, bool throwException = true)
{
var skipped = lines.SkipWhile(l => l.text != nameOfField);

switch (throwException)
{
case true:
var enumerator = lines.GetEnumerator();

while (enumerator.MoveNext())
{
if (enumerator.MoveNext())
return skipped.Skip(1).First().text;
else
throw new InvalidDataException("Odd number of items found in IEnumerable<>");
}

break;
case false:
// Skip(#) to skip over the unnecessary Lines,
// such as "Account Information", preceding "Practice Name".
return skipped.Skip(1).First().text;

break;
default:
Console.WriteLine("Default case");
break;
}
// Returning null isn't recommended, but it does fix the error "not all code paths return a value"
return null;
}

using (var p = new ChoJSONReader(jsonInputFile).WithJSONPath("$..readResults"))
{
using (var w = new ChoCSVWriter(csvFile)//.WithFirstLineHeader())
.WithField("FileName", fieldName: "File Name")
.WithField("Page")
.WithField("PracticeName", fieldName: "Practice Name")
.WithField("OwnerFullName", fieldName: "Owner Full Name")
.WithField("OwnerEmail", fieldName: "Owner Email")
.WithFirstLineHeader()
)
{
// Limit the result to page 1 since the fields below only exist on the 1st page
w.Write(p
.Where(r1 => r1.page == 1)
.Select(r1 =>
{
var lines = (dynamic[])r1.lines;
return new
{
FileName = inputFile,
Page = r1.page,
PracticeName = //lines[6].text,
fieldValue(lines, "Practice Name"),
OwnerFullName = //lines[8].text,
fieldValue(lines, "Owner Full Name"),
OwnerEmail = //lines[10].text,
fieldValue(lines, "Owner Email"),
};
}
));
}
}


Sample JSON:

{
"status": "succeeded",
"createdDateTime": "2020-11-06T18:13:45Z",
"lastUpdatedDateTime": "2020-11-06T18:13:46Z",
"analyzeResult": {
"version": "3.0.0",
"readResults": [
{
"page": 1,
"angle": 0,
"width": 8.5,
"height": 11,
"unit": "inch",
"lines": [
{
"boundingBox": [
0.2447,
0.1597,
3.6868,
0.1597,
3.6868,
0.265,
0.2447,
0.265
],
"text": "DocuSign Envelope ID: 123456",
"words": [
{
"boundingBox": [
0.2447,
0.1597,
0.71,
0.1597,
0.71,
0.265,
0.2447,
0.265
],
"text": "DocuSign",
"confidence": 1
},
{
"boundingBox": [
0.7572,
0.161,
1.207,
0.161,
1.207,
0.2638,
0.7572,
0.2638
],
"text": "Envelope",
"confidence": 1
},
{
"boundingBox": [
1.2527,
0.161,
1.3744,
0.161,
1.3744,
0.2418,
1.2527,
0.2418
],
"text": "ID:",
"confidence": 1
},
{
"boundingBox": [
1.424,
0.1597,
3.6868,
0.1597,
3.6868,
0.2431,
1.424,
0.2431
],
"text": "123456",
"confidence": 1
}
]
},
{
"boundingBox": [
0.6198,
0.4099,
2.778,
0.4209,
2.7669,
0.9194,
0.6198,
0.8973
],
"text": "FirmA",
"words": [
{
"boundingBox": [
0.6198,
0.4099,
2.7669,
0.432,
2.7669,
0.9305,
0.6198,
0.9084
],
"text": "FirmA",
"confidence": 0.638
}
]
},
{
"boundingBox": [
5.4674,
0.3323,
7.36,
0.3212,
7.36,
0.8973,
5.4674,
0.9084
],
"text": "BestBrand",
"words": [
{
"boundingBox": [
5.4785,
0.3434,
7.3047,
0.3212,
7.3047,
0.9084,
5.4674,
0.9194
],
"text": "BestBrand",
"confidence": 0.955
}
]
},
{
"boundingBox": [
5.4785,
1.0524,
7.3822,
1.0413,
7.3822,
1.2185,
5.4785,
1.2296
],
"text": "independent. together.\"™",
"words": [
{
"boundingBox": [
5.4785,
1.0524,
6.4967,
1.0634,
6.5078,
1.2296,
5.4785,
1.2185
],
"text": "independent.",
"confidence": 0.958
},
{
"boundingBox": [
6.5299,
1.0634,
7.3822,
1.0413,
7.3932,
1.2075,
6.541,
1.2296
],
"text": "together.\"™",
"confidence": 0.638
}
]
},
{
"boundingBox": [
0.5016,
1.6537,
2.5722,
1.6537,
2.5722,
1.8137,
0.5016,
1.8137
],
"text": "Account Information",
"words": [
{
"boundingBox": [
0.5016,
1.6741,
1.3397,
1.6741,
1.3397,
1.8137,
0.5016,
1.8137
],
"text": "Account",
"confidence": 1
},
{
"boundingBox": [
1.3973,
1.6537,
2.5722,
1.6537,
2.5722,
1.8137,
1.3973,
1.8137
],
"text": "Information",
"confidence": 1
}
]
},
{
"boundingBox": [
1.7716,
2.2251,
2.8791,
2.2251,
2.8791,
2.3447,
1.7716,
2.3447
],
"text": "Practice Name",
"words": [
{
"boundingBox": [
1.7716,
2.2251,
2.3801,
2.2251,
2.3801,
2.3447,
1.7716,
2.3447
],
"text": "Practice",
"confidence": 1
},
{
"boundingBox": [
2.436,
2.2344,
2.8791,
2.2344,
2.8791,
2.3447,
2.436,
2.3447
],
"text": "Name",
"confidence": 1
}
]
},
{
"boundingBox": [
3.0397,
2.2579,
3.2198,
2.2579,
3.2198,
2.3638,
3.0397,
2.3638
],
"text": "Dr",
"words": [
{
"boundingBox": [
3.0397,
2.2579,
3.2198,
2.2579,
3.2198,
2.3638,
3.0397,
2.3638
],
"text": "Dr",
"confidence": 1
}
]
},
{
"boundingBox": [
3.3462,
2.2333,
5.523,
2.2333,
5.523,
2.3978,
3.3462,
2.3978
],
"text": "Bob & Co",
"words": [
{
"boundingBox": [
3.3462,
2.2579,
4.219,
2.2579,
4.219,
2.3978,
3.3462,
2.3978
],
"text": "Bob",
"confidence": 1
},
{
"boundingBox": [
4.3355,
2.2384,
4.434,
2.2384,
4.434,
2.3666,
4.3355,
2.3666
],
"text": "&",
"confidence": 1
},
{
"boundingBox": [
4.5352,
2.2333,
5.523,
2.2333,
5.523,
2.3656,
4.5352,
2.3656
],
"text": "Co",
"confidence": 1
}
]
},
{
"boundingBox": [
1.6116,
2.7395,
2.8813,
2.7395,
2.8813,
2.8554,
1.6116,
2.8554
],
"text": "Owner Full Name",
"words": [
{
"boundingBox": [
1.6116,
2.7435,
2.1025,
2.7435,
2.1025,
2.8552,
1.6116,
2.8552
],
"text": "Owner",
"confidence": 1
},
{
"boundingBox": [
2.1539,
2.7395,
2.3782,
2.7395,
2.3782,
2.8554,
2.1539,
2.8554
],
"text": "Full",
"confidence": 1
},
{
"boundingBox": [
2.4382,
2.7448,
2.8813,
2.7448,
2.8813,
2.8551,
2.4382,
2.8551
],
"text": "Name",
"confidence": 1
}
]
},
{
"boundingBox": [
3.0246,
2.7772,
3.4989,
2.7772,
3.4989,
2.8882,
3.0246,
2.8882
],
"text": "BOB",
"words": [
{
"boundingBox": [
3.0246,
2.7772,
3.4989,
2.7772,
3.4989,
2.8882,
3.0246,
2.8882
],
"text": "BOB",
"confidence": 1
}
]
},
{
"boundingBox": [
4.3206,
2.7772,
5.1956,
2.7772,
5.1956,
2.8882,
4.3206,
2.8882
],
"text": "LEE",
"words": [
{
"boundingBox": [
4.3206,
2.7772,
5.1956,
2.7772,
5.1956,
2.8882,
4.3206,
2.8882
],
"text": "LEE",
"confidence": 1
}
]
},
{
"boundingBox": [
1.945,
3.2459,
2.8746,
3.2459,
2.8746,
3.3657,
1.945,
3.3657
],
"text": "Owner Email",
"words": [
{
"boundingBox": [
1.945,
3.2539,
2.4358,
3.2539,
2.4358,
3.3657,
1.945,
3.3657
],
"text": "Owner",
"confidence": 1
},
{
"boundingBox": [
2.4873,
3.2459,
2.8746,
3.2459,
2.8746,
3.3655,
2.4873,
3.3655
],
"text": "Email",
"confidence": 1
}
]
},
{
"boundingBox": [
3.0549,
3.282,
4.6426,
3.282,
4.6426,
3.4465,
3.0549,
3.4465
],
"text": "bob@gmail.com",
"words": [
{
"boundingBox": [
3.0549,
3.282,
4.6426,
3.282,
4.6426,
3.4465,
3.0549,
3.4465
],
"text": "bob@gmail.com",
"confidence": 1
}
]
}
]
}
]
}
}

Continue reading...
 
Back
Top