Determining which code line threw the exception

前端 未结 4 1403
故里飘歌
故里飘歌 2021-01-05 11:56

In dotNet a line throws an exception and is caught, how can I figure out which line in which file threw the exception? Seems relatively straightforward, but I can\'t figure

4条回答
  •  醉酒成梦
    2021-01-05 12:08

    You could use the StackFrame Class:

    try
    {
        ...
        ...
    
    }
    catch(...)
    {
        StackFrame sf = new StackFrame(true);
    
        int lineNumber = sf.GetFileLineNumber();
        int colNumber = sf.GetFileColumnNumber();
        string fileName = sf.GetFileName();
        string methodName = sf.GetMethod().Name;
    }
    

提交回复
热议问题