I am trying to get details from an exception occurred in my c# (4.0) asp.net web application. I have .pdb file in bin folder. The following code is not working as expected -
protected void Button1_Click(object sender, EventArgs e)
{
try
{
//throwing Exception
using (SqlConnection connection=new SqlConnection(""))
{
connection.Open();
}
}
catch (Exception exception)
{
//Get a StackTrace object for the exception
StackTrace st = new StackTrace(exception, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0); //returns {PermissionDemand at offset 5316022 in file:line:column <filename unknown>:0:0}
//Get the file name
string fileName = frame.GetFileName(); //returns null
//Get the method name
string methodName = frame.GetMethod().Name; //returns PermissionDemand
//Get the line number from the stack frame
int line = frame.GetFileLineNumber(); //returns 0
//Get the column number
int col = frame.GetFileColumnNumber(); //returns 0
}
}
What is wrong here?
update: "StackFrame frame = st.GetFrame(st.FrameCount - 1); " solved this issue.
Simple way, use the Exception. ToString() function, it will return the line after the exception description. You can also check the program debug database as it contains debug info/logs about the whole application.
The printStackTrace() method in Java is a tool used to handle exceptions and errors. It is a method of Java's throwable class which prints the throwable along with other details like the line number and class name where the exception occurred.
When an exception is thrown using the throw keyword, the flow of execution of the program is stopped and the control is transferred to the nearest enclosing try-catch block that matches the type of exception thrown. If no such match is found, the default exception handler terminates the program.
//Remove this line
StackFrame frame = st.GetFrame(0);
//Add this line
StackFrame frame = st.GetFrame(st.FrameCount - 1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With