相关疑难解决方法(0)

在异常处理中显示行号

如何显示哪个行号导致错误,这是否可能与.NET编译其.exes的方式有关?

如果没有,Exception.Message是否有自动方式显示被淘汰的子?

try
{
  int x = textbox1.Text;
}
catch(Exception ex)
{
     MessageBox.Show(ex.Message);
}
Run Code Online (Sandbox Code Playgroud)

.net c# exception-handling exception line-numbers

19
推荐指数
2
解决办法
5万
查看次数

异常处理 - 显示发生错误的行号?

可能重复:
在异常处理中显示行号

有人可以告诉我如何获取发生错误的代码的行号并将其显示到控制台?

其他信息,如文件名或方法名称将非常方便.

c# exception line show

11
推荐指数
3
解决办法
3万
查看次数

C# - 调用异常时如何获取类名和行号

我需要两个方法,一个用于从调用异常的位置获取Class,另一个用于获取调用异常的行号.

到目前为止,我有这个代码,它将类名和行号一起给我(例如:DatabaseHandler.cs:第70行):

    private string GetClassAndLine()
    {
        string tempName = e.GetBaseException().ToString();
        int tempPosition = 0;
        int length = tempName.Length;
        for (int i = 0; i < length; i++)
        {
            if (tempName.ElementAt(i).Equals('\\'))
            {
                tempPosition = i + 1;
            }
        }
        return tempName.Substring(tempPosition, length - tempPosition);
    }
Run Code Online (Sandbox Code Playgroud)

所以如果你有任何想法我怎么能单独得到它们,那将会有很大的帮助.然后将它们传递到Oracle以存储发生的任何异常.

更新2:

我正在测试此代码,正如一些人建议的那样:

        private string GetClassName()
    {
        System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(e, true); 
        return trace.GetFrame(0).GetMethod().ReflectedType.FullName;
    }

    private int GetLineNumber()
    {
        System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(e, true); 
        return trace.GetFrame(0).GetFileLineNumber();
    }
Run Code Online (Sandbox Code Playgroud)

这是在特定数据库异常时返回的内容.没有行号被触发的行号或类名.我怎么能得到它?

    Error was found at Class: Oracle.DataAccess.Client.OracleException. …
Run Code Online (Sandbox Code Playgroud)

c# asp.net

11
推荐指数
2
解决办法
2万
查看次数

UWP应用程序的堆栈跟踪中的行号

我在Visual Studio 2015中创建了一个空白的Universal Windows App项目。

我添加类似:

try {
    string foo = null;
    int len = foo.Length;
} catch (Exception ex) {
    System.Diagnostics.Debug.WriteLine(ex);
}
Run Code Online (Sandbox Code Playgroud)

我得到如下的堆栈跟踪:

Exception thrown: 'System.NullReferenceException' in TestStackTraces.exe
System.NullReferenceException: Object reference not set to an instance of an object.
at TestStackTraces.App.OnLaunched(LaunchActivatedEventArgs e)
Run Code Online (Sandbox Code Playgroud)

即没有行号。

如何显示行号?

c# visual-studio win-universal-app .net-core

7
推荐指数
1
解决办法
975
查看次数