December 9, 2007

Difference between throw and throw ex

When you rethrow an exception, in .NET there's a trap when you use 'throw ex'. You could think that 'throw' and 'throw ex' do exactly the same thing. It's not right, because when you use 'throw ex', you loose precious informations, a part of the original stack trace.

Here is the demonstration :

File Program.cs
1: using System;
2:  
3: namespace ExApp
4: {
5:     class Program
6:     {
7:         static void Main(string[] args)
8:         {
9:             try
10:             {
11:                 MyProcessing();
12:             }
13:             catch (Exception ex)
14:             {
15:                 Console.WriteLine(String.Format("[ERROR] The processing has failed : {0}\n",ex.Message));
16:                 Console.WriteLine("STACK TRACE : " + ex.StackTrace);
17:  
18:             }
19:             Console.WriteLine("PRESS A KEY TO FINISH");
20:             Console.ReadLine();
21:         }
22:  
23:         private static void MyProcessing()
24:         {
25:             try
26:             {
27:                 MySubProcessing();
28:             }
29:             catch (Exception ex)
30:             {
31:                 // we should do something here, for example, close a file
32:                 // or rollback a transaction
33:  
34:                 throw ex;
35:             }
36:         }
37:  
38:         private static void MySubProcessing()
39:         {
40:             throw new ApplicationException("An error has occured");
41:         }
42:  
43:     }
44: }


Result using 'throw ex' instead of 'throw' at the line 34
[ERROR] The processing has failed : An error has occured

STACK TRACE :
   at ExApp.Program.MyProcessing() in C:\src\ExApp\Program.cs:line 34
   at ExApp.Program.Main(String[] args) in C:\src\ExApp\Program.cs:line 11

PRESS A KEY TO FINISH

Result using 'throw' at the line 34
[ERROR] The processing has failed : An error has occured

STACK TRACE :
   at ExApp.Program.MySubProcessing() in C:\src\ExApp\Program.cs:line 40
   at ExApp.Program.MyProcessing() in C:\src\ExApp\Program.cs:line 34
   at ExApp.Program.Main(String[] args) in C:\src\ExApp\Program.cs:line 11

PRESS A KEY TO FINISH


In the first case, we just know that the issue is somewhere in the try block of the MyProcessing function, while in the second case we have the right line in the right function (MySubProcessing line 40).

It's not a bug in C#, it's logical in fact. 'throw ex' throw a new exception while 'throw' rethrow exactly the exception catched previously.

Just be careful with that because it can be a real problem when you loose several levels of stack trace. It's complex to locate an issue in that case.

December 8, 2007

ASP.NET basic best practices

As you can see around you, thin or rich clients are more and more used to develop complex applications such as ERP softwares for example. This kind of user interface is not reserved to promotional or shopping websites anymore.

A couple of years ago, it wasn't very interesting to use thin clients in a complex software because we wasn't able to have enough client-side interactions. For users, this type of UI was too slow and their experience when they used the software wasn't pleasant. Moreover is was very complicated to take into consideration every browsers particularities.

Today, with Ajax, those problems begin to disappear and according to me when you write a new and large application with complex UI interactions, rich client may be a suitable solution that we have to take into account.

That's why, even if I don't plan to become a webmaster, I have been looking at ASP.NET very seriously for several months. I have found a lot of interesting things, I can't do the whole list in my blog, but these are the most important :

  • integrated XML markups are more and more used (for example <asp:button>) and it's better to use them when it's possible instead of using an HTML equivalent
  • CSS must be used as much as possible, it can really improve the quality of your web site
  • DIV and SPAN markups can be used without moderation
  • You have to use bindings as much as possible
  • You have to avoid to generate HTML by code because a designer won't be able to improve your interface if you do that
I don't explain those predicates in more details because I don't pretend to have made an incredible discovery. But I would be happy to be more accurate if needed.

Nico

December 7, 2007

Microsoft talks about Sogeti

Microsoft, on his international web site, relates a success story of a well known french IT company called Sogeti. Sogeti is a subsidiary of Cap Gemini. The author explains how it's easy to design an application with VS2008 Team System. More

December 6, 2007

Primer numbers list using LINQ

Today as I was consulting my numerous rss headlines, I found a very interesting article from raptorxp a rival of mine.

A good one because the topic of his experience was to produce a prime numbers list using LINQ. And that's not all, using only three clauses !

I thought : "noooooo it's impossible, he can't do that."

He did it.

The answer on his blog : here

Congratulation raptor, you rocks

 
Posts Customize