我试图用下面的代码捕获异常细节,但它没有给出细节错误。
我试图从我们的 ftp 服务器下载一个文件,其中包含 ftp 类之类的代码。当找不到文件时,我触发上述方法并传入异常。不幸的是,该方法打印了以下内容:
Error occured and Error details as follows
Exception occured in file
Exception occured in method SyncRequestCallback
Exception occured in line & column number 0 0
这是我的代码。如何捕获异常详细信息(例如行号、引发异常的方法等)?
public static string GetException(Exception ex)
{
//Get a StackTrace object for the exception
StackTrace st = new StackTrace(ex, true);
//Get the first stack frame
StackFrame frame = st.GetFrame(0);
//Get the file name
string fileName = frame.GetFileName();
//Get the method name
string methodName = frame.GetMethod().Name;
//Get the line number from the stack frame
int line = frame.GetFileLineNumber();
//Get the column number
int col = frame.GetFileColumnNumber();
string strBody = "Hi,<br/><br/>";
strBody = strBody + "Error occured and Error details as follows<br/>";
strBody = strBody + "Error message " + ex.Message + "<br/>";
strBody = strBody + "Exception occured in file " + fileName + "<br/>";
strBody = strBody + "Exception occured in method " + methodName + "<br/>";
strBody = strBody + "Exception occured in line & column number " + line + " " + col + "<br/><br/>Thanks";
return strBody;
}
谢谢