Let's say I have a class Person with different attributes of different datatypes, and I have this code in my main method:
Person p1 = new Person
{
Name = inputName,
DateOfBirth = inputDOB,
Height = height,
Weight = weight,
Passport = inputPassport
};
Now, let's say the assignation of Height throws an exception, for example an InvalidCastException, the code will stop executing, and probably log something in case I have a logging table. The issue would be that from the log I won't be able to understand exactly which line and assignation throw the exception.
Is there any way to capture the exception and being able to log (somewhere in the database) which assignation throw the exception and for which type of cast in this case (example double to DateTime).
UPDATE: At the end I resolved my problem by doing this:
- Updating the code so that it first creates the object (without setting the properties) and then sets the properties one line at a time.
- Between every line add a string that was concatenating the value of the variable just set.
- In my catch, I was logging the concatenating string, so having a quick look at that string basically tells me which was the last line of code that has been successfully executed before throwing the exception.