thrown exception lists the line number of throw

D

Dan Holmes

Server stack trace:
at IVS.Framework.ControlNumberService.InstallComponent(Identity id,
ControlNumberInfo ctrlNumber) in
C:\Sandbox\IAF.NET\IAF\trunk\src\BOs\ControlNumbers\Server\ControlNumberServer.cs:line
86


The line number 86 is the "throw" line:

catch (Exception ex)
{
ex.Data.Add("SQL", sql.ToString());
throw ex;
}

How can i get the line number to be the line that threw instead of the
number that caught and rethrew?

I have tried just throw and throw ex.

dan
 
D

Dan Holmes

If the try catch is removed the correct line number is displayed. I
don't want to remove the catch i just want the correct line number.
 
D

Dan Holmes

Mark said:
You said that you tried just "throw", this should work since "throw ex" will
recalculate the stacktrace information, whereas throw will just keep raising
the exception without modifying the stacktrace.

Can you verify again using just throw and if it is not doing what you expect
post a short sample.

Thanks
Mark.

This is the output of the following example.

[c:\temp\exceptiontest\exceptiontest\bin\debug]ExceptionTest.exe
at ExceptionTest.Program.TestMath.MathTest(Int32 denominator) in
C:\temp\ExceptionTest\ExceptionTest\Program.cs:line
32
at ExceptionTest.Program.Main(String[] args) in
C:\temp\ExceptionTest\ExceptionTest\Program.cs:line 14

Line 32 is the "throw" in MathTest. Removing the (Exception ex) doesn't
change the output.

using System;
using System.Collections.Generic;
using System.Text;

namespace ExceptionTest
{
class Program
{
static void Main(string[] args)
{
try
{
TestMath tm = new TestMath();
Console.WriteLine(tm.MathTest(0));
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
internal class TestMath
{
public double MathTest(int denominator)
{
try
{
return 1 / denominator;
}
catch (Exception ex)
{
ex.Data.Add("denominator", denominator);
throw;
}
}
}
}
}
 
B

Ben Voigt

internal class TestMath
{
public double MathTest(int denominator)
{
try
{
return 1 / denominator;
}
catch (Exception ex)
{
ex.Data.Add("denominator", denominator);
throw;
}
}

You could use a wrapper exception instead, so that the InnerException
property will have the original stack trace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top