How can I get line numbers to print when a run-time exception is thrown and the executable is compiled in Release mode? Normally, you lose the line numbers while printing EX.ToString, if the build was made in Release Mode.
You can easily get line numbers printed in your application, even in Release mode, by setting a property in the Build Configuration. To get to this parameter, right click on the project file in the Project Explorer of your assembly. This will cause the Project Properties window to be displayed as shown in Figure 1. Next, click the Configuration Properties folder, and then click the Build property. Once you are on the Build properites, check the Generate debugging information checkbox. This will cause the line number information to be generated in the .PDB, or debugging database. If you do this while you are building in Debug mode, multiple users will not be able to run the application at the same time because the first instance of the application will lock the .PDB file and no other instance of the program will be able to execute. However, this is not the case in Release mode. Multiple users can execute the application, using the PDB, when the assembly is built in Release mode.
Figure 1 shows the setting that you need to set to be able to have line numbers print in an exception handler.
Figure 1. Setting the Build parameters for Line Numbers

The code shown below, will cause an exception as soon as the Form1 loads. The assembly was build in Release mode, and the line numbers will show in the exception message box.
| 45 Private Sub Form1_Load(ByVal sender As System.Object, _ 46 ByVal e As System.EventArgs) Handles MyBase.Load 47 TestException() 48 End Sub 49 Private Sub TestException() 50 ' this code will generate an exception 51 Try 52 Dim s As String 53 s = s.Replace("'", "") 54 Catch ex As Exception 55 MessageBox.Show(ex.ToString, "Exception Caused on Purpose", _ 56 MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 57 End Try 58 End Sub |
