Improve Exception Messages
- Dominant language
- C#
- Stars
- 989
- Forks
- 340
- Avg merge
- 4d 19h
- Merged PRs (30d)
- 72
Description
Note in the follow snippet that there isn't a column index 4 in the query. When we run the code, we got a `IndexOutOfRangeException`. But when the query and object initializer is more complex, is difficult to locate the problem.
My suggestion here is to include in exception message what's the column index that not exists.
`Unhandled Exception: System.IndexOutOfRangeException: Index was outside the bounds of the array.
at Microsoft.Data.SqlClient.SqlDataReader.CheckDataIsReady(...)`
using (var conn = new SqlConnection("Data Source=.;Initial Catalog=Test;Integrated Security=True"))
{
using (var cmd = new SqlCommand("SELECT Id, Name, Created, Token FROM People", conn))
{
conn.Open();
using (var dr = cmd.ExecuteReader())
{
while (dr.Read())
{
people.Add(new Person()
{
Id = dr.GetInt32(0),
Name = dr.GetString(1),
Created = dr.GetDateTime(2),
Token = dr.GetGuid(3),
Dummy = dr.GetString(4)
});
}
}
}
}
The same approach could be used when the problem is casting `InvalidCastException`:
`Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
at Microsoft.Data.SqlClient.SqlBuffer.get_String() `
Contributor guide
Assessment
This issue has not been assessed yet.