Throw without parameter...

A

AshokG

Hi,

If you use just throw without parameter should preserve the complete stack
trace and the exception information.

for example:

1. private void Bar()
2. {
3. try
4. {
5. string s = null;
6. if (s.Length == 0)
7. return;
8. }
9. catch
10. {
11. // do some processing...
12. throw;
13. }
14. }
15.
16.private void Foo()
17.{
18. try
19. {
20. Bar();
21. }
22. catch (Exception ex)
23. {
24. // do some processing...
25. Console.WriteLine(ex.ToString())
26. }
27.}

In the above code it should show error line nos 6, 12 and 20 in the stack
trace. But now in newer versions it is just showing the line nos. 12 and 20.

It seems this functionality is removed or a bug in newer versions. It's no
longer preserving the stack trace info. I saw this In .NET l.0 framework
(VS.NET 2002).


Regards,
Ashok
 
A

Alberto Poblacion

AshokG said:
If you use just throw without parameter should preserve the complete stack
trace and the exception information.
[...] In the above code it should show error line nos 6, 12 and 20 in the
stack trace. But now in newer versions it is just showing the line nos. 12
and 20.

It could be that the optimizer in the compiler is inlining the function
that you are calling. Therefore, there is no longer a "line 6" -- the
function that contained line 6 has moved into line 12.
 
R

RB

AshokG said:
Hi,

If you use just throw without parameter should preserve the complete stack
trace and the exception information.

for example:

1. private void Bar()
2. {
3. try
4. {
5. string s = null;
6. if (s.Length == 0)
7. return;
8. }
9. catch
10. {
11. // do some processing...
12. throw;
13. }
14. }
15.
16.private void Foo()
17.{
18. try
19. {
20. Bar();
21. }
22. catch (Exception ex)
23. {
24. // do some processing...
25. Console.WriteLine(ex.ToString())
26. }
27.}

In the above code it should show error line nos 6, 12 and 20 in the stack
trace. But now in newer versions it is just showing the line nos. 12 and 20.

It seems this functionality is removed or a bug in newer versions. It's no
longer preserving the stack trace info. I saw this In .NET l.0 framework
(VS.NET 2002).


Regards,
Ashok

Java would certainly preserve the stack trace, but I've never known .NET
to do that.

In .NET I tend to use nested exceptions for this functionality as it
will print out the stack trace of the caught exception, and all nested
exceptions.

So, your line 9 -> 13 becomes:

catch (Exception ex)
{
throw new Exception("I'm nesting an exception", ex)
}

Cheers,

RB.
 
F

Frans Bouma [C# MVP]

AshokG said:
Hi,

If you use just throw without parameter should preserve the complete
stack trace and the exception information.

for example:

1. private void Bar()
2. {
3. try
4. {
5. string s = null;
6. if (s.Length == 0)
7. return;
8. }
9. catch
10. {
11. // do some processing...
12. throw;
13. }
14. }
15.
16.private void Foo()
17.{
18. try
19. {
20. Bar();
21. }
22. catch (Exception ex)
23. {
24. // do some processing...
25. Console.WriteLine(ex.ToString())
26. }
27.}

In the above code it should show error line nos 6, 12 and 20 in the
stack trace. But now in newer versions it is just showing the line
nos. 12 and 20.

It seems this functionality is removed or a bug in newer versions.
It's no longer preserving the stack trace info. I saw this In .NET
l.0 framework (VS.NET 2002).

Isn't there an inner exception in 'ex' in Foo ?

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
C

Cor Ligthert[MVP]

">
I fail to see how that has anything at all to do with the question.
I fail to see why you are sending this message, the code does not even
compile in VB.Net.

Cor
 
A

Alberto Poblacion

Cor Ligthert said:
I fail to see why you are sending this message, the code does not even
compile in VB.Net.

That's one of the reasons why cross-posting to multiple groups is
discouraged. The original poster sent the message (which contained some code
in C#) to multiple groups, including the VB group, where of course the code
won't even compile.
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Cor said:
">
I fail to see why you are sending this message, the code does not even
compile in VB.Net.

Cor

What code? I didn't post any code. You are not making any sense at all.
 

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