How to handle global error with exact line number RRS feed

  • Question

  • User-1339639702 posted

    Hi Team,

    I am using global.asax page to handle any exception in the page. So when any exception is occurred in any of the method inside page that error is catch by those specific method and exception throw to global.asax page using that. Inside global.asax i am logging those exception. using below code:

    Exception exception = server.GetLastError().GetBaseException();

    In this case i am getting the method name like in which method exception got occurred but in stack trace line number is getting changed.

    For e.g: I have providing one scenario below:

    I have one abc method where i got the exception in line number 416.

    in this method i used try catch block now in catch block exception got catched and there i have throw keyword which again throw that exception to global.asax page.

    When throw keyword throws the exception in that stack trace exact line number is changing to the throw keyword line number, And due to it is very hard to understand in which line this exception occurred.

    Kindly suggest on above.



    Tuesday, July 2, 2019 12:10 PM

All replies

  • User753101303 posted


    More likely you are using throw ex; which destroy the call stack. Use throw; instead. See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/throw#re-throwing-an-exception (and note the "Important"  remark).

    Not directly related but I prefer to log full details (ie all the exception chain ie not just the outer exception but also not just the base exception) using for example server.GetLastError().ToString()

    Tuesday, July 2, 2019 12:23 PM
  • User-1339639702 posted

    Hi PatriceSC, 

    I used only throw keyword in catch exception not throw ex.

    Tuesday, July 2, 2019 12:35 PM
  • User753101303 posted

    Maybe you have a similar exception that happens just before throw? Can you show us few lines so that we get a basic idea of what you are doing and check if we see the same behavior on our side? AFAIK it is not supposed to happen and I never heard anyone reporting this behavior.

    Tuesday, July 2, 2019 1:02 PM
  • User-1339639702 posted

    Sure PatriceSC,

     protected void btnViewReport_Click(object sender, EventArgs e)
    if (cmbInsType.Text.ToLower() == "claims".ToLower() && cmbClaimType.SelectedItem!=null)
                              Exception catches here: searchParam.ClaimType = Convert.ToString(cmbClaimType.SelectedItem.Text);//Line number:6
    catch(Exception ex)
      Exception throws here: throw;Line number:11

    As can see in above specific code i have mentioned in the piece of code where exception occurs having line number:6 and after catch exception when throw keyword throws again that exception (having line number: 11) to Application_Error method inside global.asax page. Where in stack trace the exact line number is changing from 6  to 11.

    Please check and suggest. 

    Tuesday, July 2, 2019 1:15 PM
  • User753101303 posted

    Ah. I do see the same behavior and based on that found a discussion at  https://github.com/dotnet/coreclr/issues/15780 with a possible .NET 4.x workaround (it seems Core 2.1 doesn't have any more this problem) based on


    What if you try ?

    Actually I refrained in my first post to suggest that line numbers are perhaps not that usefull. AFAIK in production it requires deploying PDB files and it can be misleading depending on which code optimizations are applied.
    I missed them at the very beginning but with some experience (and if avoiding very long methods) it is often quite easy to find the problem without a line number (in rare cases you can add a trace which make me think also that with the correct attribute you should be able to explictely collect the actual line number in traces).

    Tuesday, July 2, 2019 2:28 PM
  • User-1339639702 posted

    Hi PatriceSc,

    You are right i have checked this line of code and and i have gone through the github link where it shows how to implement this code and i checked and found the same line number present when exception occurred in that piece of code.

    Now i have here one more question: in my project i am using some BA files to execute the procedure and getting the result in that method i also used this piece of code but it is not working there throwing error in that line :

    Error is like ExceptionDispathInfo is not exist in namespace System.Runtime.ExceptionServices.

    Can we use the suggested code in BA files?

    Wednesday, July 3, 2019 9:11 AM
  • User753101303 posted

    What means "BA" ?

    Wednesday, July 3, 2019 9:43 AM
  • User-1339639702 posted

    I am using in my project separate class library to maintain the data layers.

    In those library names callled BA and have BA.cs file

    In this file i have some procedure calling method to get the data from list and return to the page.

    in this file i used try catch block where when i add this piece of code getting build error as mentioned in previous post.

    Hope you understand.

    Wednesday, July 3, 2019 9:54 AM
  • User753101303 posted

    Ok, just C# code then. I now noticed ExceptionDispatchInfo (c is missing or is this a typo in your post) ? Also according to the documentation, it needs .NET 4.5 or later...

    Wednesday, July 3, 2019 10:36 AM
  • User-1339639702 posted

    PatriceSC, It is typo mistake. 

    Yes you was right class library project was referring 4.0 .Net framework version only , After changing 4.0 to 4.5.2. This piece of code is working fine.

    Your support is much appreciated.



    Friday, July 5, 2019 11:13 AM