« Back to blog

throw; ou throw ex; in .Net

Je voulais catcher une exception pour la loger mais ensuite continuer le traitement, et je pensais quand utilisant throw ex j'allais continuer la propagation de celle-çi.

Mais en faite pas vraiment, car implicitement le framework relance un exception ce qui fait que l'on perd le contexte (Stack trace par exemple).

Donc en cherchant un peu il suffit d'utiliser throw tout court ;-)

Basically MSIL (CIL) has two instructions - "throw" and "rethrow" and guess what

  • C#'s "throw ex;" gets compiled into MSIL's "throw"
  • C#'s "throw;" into MSIL "rethrow"!

[...] So you just better know this stuff - use "throw;" to re-throw an exception in .NET.

Source : http://www.tkachenko.com/blog/archives/000352.html