我試圖用下面的代碼捕獲異常詳細信息,但沒有提供詳細的錯誤信息。關於異常詳細信息的問題c#
我試圖從我們的ftp服務器下載一個類似ftp類的代碼的文件。當找不到文件時,我會激發上述方法並傳入異常。不幸的是,這種方法打印如下:
這是我的代碼。如何捕獲異常詳細信息(例如行號,拋出異常的方法等)?
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;
}
感謝
爲什麼不直接使用'ex.ToString()'? – 2013-02-28 14:25:53
此外,您還應該檢查內部異常,因爲錯誤可能是由於引發另一個錯誤而導致的。 – Justin 2013-02-28 14:27:29
好吧,我看到一個方法名稱:'SyncRequestCallback'。您應該檢查整個調用堆棧以查找異常的根本原因。 – sloth 2013-02-28 14:28:42