none
Show file name and line number when an exception occurs in 'release' version of software RRS feed

  • Question

  • Hi Folks,

    I have an Excel addin developed in VS2010. I have put Try ... Catch blocks throughout the code in case of an unexpected exception. This addin is deployed to a website where other users can download the addin (see http://www.statisticianaddin.com/download-page.html). One of my users has had unexpected exception occur which I cannot replicate on the development machine. It would be really handy to know in which function/subroutine the exception occurs and also at which line number the error is. To do this, I have deployed a file called PBD to the website. I am deploying with ClickOnce and running under .net 3.5. The problem is, when the exception is thrown I do not get the information I need. I am using the following code.

     

    Sub HandleException(ByVal ex As Exception)
    
    MessageBox.Show(ex.StackTrace.ToString)
    
    End Sub

    However ex.StackTrace.ToString does show file name and a line number when an exception is thrown on the development machine.

    Can anyone suggest what I may be doing wrong?

    Thanks,

    Bernie



    Bernie

    Tuesday, March 5, 2013 7:30 AM

All replies

  • first of all - release build (compiled code) may not correspond one-to-one to your source code as compiler may decide to throw away or rewrite some lines, so code line is out of question for release build. As for file name - what do you need this for? exception shows exactly which class and function barfed so what's the point of file name? i do not recall vb.net to allow writing single function across multiple files.

    So if you want line number, deploy debug build and make sure that in clickonce manifest pdb files are included (or they will not get downloaded to client)

    Tuesday, March 5, 2013 7:51 AM
  • Thanks DamienD,

    OK, could I somehow obtain specific instructions to implement your suggestion. I cannot find any links that are helpful. Could you suggest one?

    Bernie


    Bernie

    Wednesday, March 6, 2013 2:17 AM
  • Bernie,

    Personally I think deploying your 'debug' version in production is -never- a good idea. Handling your exception in a proper way should give you enough information to lean on.

    Logging

    Make sure you create a 'default' exception handler where you can put as much information as possible to research your issues. I recommend looking at the Enterprise Library Logging Application Block to write your output to either a log file or event in the event viewer

    Exception Handling

    That said, to solve your initial problem, start with adding enough information to your exception handler to recognize what the function or method is that throws the error. Don't just depend on the StackTrace but also include the other Exception properties. You will already get a lot of information by just showing the exception.ToString() information.

    Now you know where/what exception handler is capturing your exception, add more information specific to that function or method. Add information on your local variables and see if there is something not right.

    Other options

    If you still are not able to manage to get enough information out there are also the more complex options like IntelliTrace (building a repro file on the client you can use to debug the issue) or Remote Debugging (attaching your development environment to the client machine). These options however can be complicated to do and you should be ok with the extended Exception Handling as I mentioned above.

    -= Maarten =-


    Software Engineer * MVP-Visual Developer-VSTO

    Wednesday, March 6, 2013 9:21 AM
    Moderator
  • Thank you Maarten & DamienD,

    Unfortunately I am still left with the original problem, how to get the linenumber of an exception in software that has been deployed. I have already included the PBD file in the solution. The ‘ex.StackTrace’ property does have the linenumber of an exception that occurs in debug mode on the development PC. I was lead to believe that if I deployed the PBD file I would be able to retrieve the line number, but unfortunately this is not the case. I am now using the following code:

    Sub HandleException(ByVal ex As Exception)
    
    		Dim tbxMsg As String = ""
    		Dim st As StackTrace = New StackTrace(ex, True)
    
    		For Each sf As StackFrame In st.GetFrames
    			tbxMsg += "========================================================================" + Environment.NewLine
    			tbxMsg += "Line =>> " & sf.GetFileLineNumber() & Environment.NewLine & "File Name =>> " & sf.GetFileName + Environment.NewLine
    		Next
    
    		MessageBox.Show(tbxMsg)
    	End Sub

    I get the following output when an exception is thrown:

    ========================================================================
    Line =>> 0
    File Name =>>
    ========================================================================
    Line =>> 0
    File Name =>>
    ========================================================================
    Line =>> 0
    File Name =>>
    ========================================================================
    Line =>> 0
    File Name =>>
    ========================================================================
    Line =>> 0
    File Name =>>

    Clearly the file name and line number are not showing.

    Thanks,

    Bernie


    Bernie

    Thursday, March 7, 2013 7:22 PM
  • if your deployed pdb _matches_ dll that is running, simply logging to file exception.ToString() will give you file and line number. If however dll and pdb does not match, you will get nothing.
    Thursday, March 7, 2013 10:09 PM
  • How did you deploy the PDB to the client?

    -= Maarten =-


    Software Engineer * MVP-Visual Developer-VSTO

    Thursday, March 7, 2013 11:46 PM
    Moderator
  • Thanks Maarten,

    I right click the project name in the solution explorer and the select Add.. Add Existing Item. I then select the PBD file. I also have Show All Items set in the solution explorer. I then right click the PBD file and select Properties from the menu and set Copy to Output Directory to Copy Always. The PBD file is definitely present in the /publish folder after I publish.

    Best,

    Bernie


    Bernie

    Friday, March 8, 2013 6:36 PM
  • Hey Bernie,

    Did you find a solution to this?

    I have been facing this issue and haven't found anything helpful on the net.

    Thanks in advance!

    Tuesday, August 18, 2015 1:40 PM
  • Hi Vicky, no I never did end up resolving this issue.

    Best Bernie


    Bernie

    Wednesday, August 19, 2015 3:46 AM
  • Thanks !
    Wednesday, August 19, 2015 9:02 AM