We want to get user defined data that has been caused exception eg. Index out of bound exception.
try
{
int[] list = new int[2];
list[0] = 1;
list[1] = 2;
list[3] = 3;
Console.WriteLine(list[4]);
}
catch (Exception ex)
{
/* Expected
1) Line Number :5
2) Method Name: SearchArray()
3) Data: 3 // object specifically
4) excetion message:ArrayIndexOutOfBound Exception.
*/
StackTrace st = new StackTrace(ex, true);
StackFrame frame = st.GetFrame(0);
var data = ex.StackTrace;
var tr = ex.HResult;
}
In this scenario, we want to get List[3] value because this line is responsible for the exception (where list[3] will not be accessed).