How to get error line number of code using try-catch

前端 未结 10 941
轮回少年
轮回少年 2020-12-05 13:08

I want to get line number of code which cause error. For example;

static void Main(string[] args)
{
    using (SqlConnection conn = new SqlConnection(bagcum)         


        
相关标签:
10条回答
  • 2020-12-05 13:37

    Here's a rather easy way to get a bunch of info from the Exception object: Just add code like this to any potentially exception-throwing methods:

    catch (Exception ex)
    {
        String exDetail = String.Format(ExceptionFormatString, ex.Message, Environment.NewLine, ex.Source, ex.StackTrace);
        MessageBox.Show(exDetail);
    }
    

    The information you get will often be more specific, especially as regards line numbers of where problems are occurring, than you would otherwise see.

    You may have noted that the String.Format() uses a constant, namely "ExceptionFormatString". This is a good practice, so that if you want to change it, after adding the above code to 40-eleven methods, you can just change it one place. Anyway, here it is:

    public static readonly String ExceptionFormatString = "Exception message: {0}{1}Exception Source: {2}{1}Exception StackTrace: {3}{1}";
    

    Happy Debugging!

    0 讨论(0)
  • 2020-12-05 13:40

    Try this simple hack instead:

    First Add this (extension) class to your namespace(most be toplevel class):

    public static class ExceptionHelper
    {
        public static int LineNumber(this Exception e)
        {
    
            int linenum = 0;
            try
            {
                //linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));
    
                //For Localized Visual Studio ... In other languages stack trace  doesn't end with ":Line 12"
                linenum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(' ')));
    
            }
    
    
            catch
            {
                //Stack trace is not available!
            }
            return linenum;
        }
    }
    

    And its done!Use LineNumber method whenever you need it:

    try
    {
    //Do your code here
    }
    catch (Exception e)
    {
    int linenum = e.LineNumber();
    }
    
    0 讨论(0)
  • 2020-12-05 13:41

    You can use the System.Diagnostics.StackTrace class as below:

    public void MethodName()
    {
        try
        {
            throw new Exception();
        }
        catch (Exception ex)
        {
            // Get stack trace for the exception with source file information
            var trace = new StackTrace(ex, true);
    
            // Get the top stack frame
            var frame = trace.GetFrame(0);
    
            // Get the line number from the stack frame
            var line = frame.GetFileLineNumber();
        }
    }
    
    0 讨论(0)
  • 2020-12-05 13:45

    In .NET 4.5 you can use the ExceptionDispatchInfo to rethrow your exceptions instead of the classic throw;(make sure the PDB files are there or no line numbers will be displayed):

        static void A()
        {
            try
            {
                throw new Exception("A");
            }
            catch (Exception e)
            {
                ExceptionDispatchInfo.Capture(e).Throw();
            }
        }
    

    Source: blogpost. PDB files don't decrease performance on Windows.

    0 讨论(0)
  • 2020-12-05 13:51

    try this

    To get the line numbers in the StackTrace, you need to have the correct debug information (PDB files) alongside your dlls/exes. To generate the the debug information, set the option in Project Properties -> Build -> Advanced -> Debug Info:

    alt text

    Setting it to full should suffice (see the MSDN docs for what the other options do). Debug info (ie. PDB files) are generated for Debug build configurations by default, but can also be generated for Release build configurations.

    Generating PDBs for release builds enables you to ship you code without the PDBs, but to drop the PDBs next to the dlls if you need line numbers (or even to attach a remote debugger). One thing to note is that in a release build, the line numbers may not be entirely correct due to optimisations made by the compiler or the JIT compiler (this is especially so if the line numbers show as 0).

    0 讨论(0)
  • 2020-12-05 13:51

    You might get 0 in result if you don't initialize StackTrace to include fileinfo.

    enter image description here

    Try this

    try
    {
        //code
    }
    catch (Exception e)
    {
        var lineNumber = new System.Diagnostics.StackTrace(e, true).GetFrame(0).GetFileLineNumber();
    }
    

    This worked for me.

    0 讨论(0)
提交回复
热议问题