`
Difference between "throw" and "throw ex" in C#
In this blog you will learn about Basic Difference between "throw" & "throw ex" in C# exception handling.
throw : If we use "throw" statement, it preserve original error stack information. In exception handling "throw" with empty parameter is also called re-throwing the last exception.
throw ex : If we use "throw ex" statement, stack trace of exception will be replaced with a stack trace starting at the re-throw point. It is used to intentionally hide stack trace information.
- catch (Exception ex)
- {
-
- throw;
- throw new MyException("failed", ex);
- throw new MyException("failed");
- throw ex;
- }
So it is good practice to use the "throw" statement, rather than "throw ex" because it will give us more accurate stack information rather than "throw ex".