C #-예외를 던진 줄 번호를 얻습니다.
A의 catch
블록, 어떻게 예외를 던져 줄 번호를받을 수 있나요?
Exception.StackTrace에서 얻은 형식이 지정된 스택 추적 이상의 행 번호가 필요한 경우 StackTrace 클래스를 사용할 수 있습니다 .
try
{
throw new Exception();
}
catch (Exception ex)
{
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(0);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
}
이것은 어셈블리에 사용 가능한 pdb 파일이있는 경우에만 작동합니다.
간단한 방법으로 Exception.ToString()
함수를 사용 하면 예외 설명 후에 줄을 반환합니다.
전체 응용 프로그램에 대한 디버그 정보 / 로그가 포함 된 프로그램 디버그 데이터베이스를 확인할 수도 있습니다.
.PBO
파일 이없는 경우 :
씨#
public int GetLineNumber(Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
Vb.net
Public Function GetLineNumber(ByVal ex As Exception)
Dim lineNumber As Int32 = 0
Const lineSearch As String = ":line "
Dim index = ex.StackTrace.LastIndexOf(lineSearch)
If index <> -1 Then
Dim lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length)
If Int32.TryParse(lineNumberText, lineNumber) Then
End If
End If
Return lineNumber
End Function
또는 Exception 클래스의 확장으로
public static class MyExtensions
{
public static int LineNumber(this Exception ex)
{
var lineNumber = 0;
const string lineSearch = ":line ";
var index = ex.StackTrace.LastIndexOf(lineSearch);
if (index != -1)
{
var lineNumberText = ex.StackTrace.Substring(index + lineSearch.Length);
if (int.TryParse(lineNumberText, out lineNumber))
{
}
}
return lineNumber;
}
}
You could include .PDB
symbol files associated to the assembly which contain metadata information and when an exception is thrown it will contain full information in the stacktrace of where this exception originated. It will contain line numbers of each method in the stack.
It works:
var LineNumber = new StackTrace(ex, True).GetFrame(0).GetFileLineNumber();
Check this one
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();
Update to the answer
// Get stack trace for the exception with source file information
var st = new StackTrace(ex, true);
// Get the top stack frame
var frame = st.GetFrame(st.FrameCount-1);
// Get the line number from the stack frame
var line = frame.GetFileLineNumber();
I tried using the solution By @davy-c but had an Exception "System.FormatException: 'Input string was not in a correct format.'", this was due to there still being text past the line number, I modified the code he posted and came up with:
int line = Convert.ToInt32(objErr.ToString().Substring(objErr.ToString().IndexOf("line")).Substring(0, objErr.ToString().Substring(objErr.ToString().IndexOf("line")).ToString().IndexOf("\r\n")).Replace("line ", ""));
This works for me in VS2017 C#.
Extension Method
static class ExceptionHelpers
{
public static int LineNumber(this Exception ex)
{
int n;
int i = ex.StackTrace.LastIndexOf(" ");
if (i > -1)
{
string s = ex.StackTrace.Substring(i + 1);
if (int.TryParse(s, out n))
return n;
}
return -1;
}
}
Usage
try
{
throw new Exception("A new error happened");
}
catch (Exception ex)
{
//If error in exception LineNumber() will be -1
System.Diagnostics.Debug.WriteLine("[" + ex.LineNumber() + "] " + ex.Message);
}
In Global.resx file there is an event called Application_Error
it fires whenever an error occurs,,you can easily get any information about the error,and send it to a bug tracking e-mail.
Also i think all u need to do is to compile the global.resx and add its dll's (2 dlls) to your bin folder and it will work!
This works for me:
try
{
//your code;
}
catch(Exception ex)
{
MessageBox.Show(ex.StackTrace + " ---This is your line number, bro' :)", ex.Message);
}
참고URL : https://stackoverflow.com/questions/3328990/c-sharp-get-line-number-which-threw-exception
'Programming' 카테고리의 다른 글
새로운 .gitignore 파일과 git repo 재 동기화 (0) | 2020.05.20 |
---|---|
테이블 변수에 인덱스 생성 (0) | 2020.05.20 |
패키지없이 클래스의 이름을 얻는 방법? (0) | 2020.05.20 |
'input'요소 앞뒤의 CSS 컨텐츠 생성 (0) | 2020.05.20 |
MySQL 루트 비밀번호를 제거하는 방법 (0) | 2020.05.20 |