Why does the C# compiler authorize "throw ex" in catch, and is there a case where "throw ex" is useful?
c#, exception
Solution
`throw new Exception();` or `throw ex;` both will use the same language rules to allow throwing an exception object, (whether new or existing). When you want to add some extra information with the exception than that option is helpful.
See: How to: Explicitly Throw Exceptions - MSDN
You can explicitly throw an exception using the throw statement. You can also throw a caught exception again using the throw statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.
Since both, `throw new Exception()` and `throw ex;` would require the same language rules, it is not really compiler's job to distinguish those two.
Simply throwing the existing exception without any modification to the exception object would be using the same language construct.
Also as @D Stanley has pointed out in his answer, that truncating the stack trace could be the desired behaviour.
As far as your question about compiler not warning about it is concerned, It is not the job of compiler to warn about bad practices, there are code analysis tools. For example Managed Code Analysis tool will raise the warning for `throw ex;` CA2200: Rethrow to preserve stack details
Problem
In C#, younger developers use often "throw ex" instead of "throw" to throw exception to parent method. Example : ``` try { // do stuff that can fail } catch (Exception ex) { // do stuff throw ex; } ``` "throw ex" is a bad practise because the stack trace is truncated below the method that failed. So it's more difficult to debug code. So the code must be : ``` try { // do stuff that can fail } catch (Exception ex) { // do stuff throw; } ``` My question is why compilator authorize this (or doesn't display a warning message ?) Is there a case which "throw ex" is useful ?