Roshan Rathod
What is difference between the "throw" and "throw ex" in .NET?
By Roshan Rathod in C# on Oct 04 2020
  • Sachin Singh
    Oct, 2020 20

    throw ex resets the stack trace while throw doesn’t.

    • 4
  • Kiran Mohanty
    Oct, 2020 6

    When we use “throw” , it will throw a new exception without carrying real error info. But when we use “throw ex”, object ex will have error info like ErrorMessage from which we can identity the real reason behind this exception.
    Another scenario, if we wish to show user-friendly error message for technical exception (like Database connect issue), we can change ErroMessage property to a user-friendly message and throw that exception as “throw ex” to client.

    • 3
  • Madhura Kesharwani
    Jul, 2021 6

    throw gives original error stack information.throw ex gives stack trace of exception will be replaced with a stack trace starting at the re-throw point. Prefer to use "throw" instead of "throw ex" because it will give accurate error stack information

    • 0
  • Deepak Nehra
    Jun, 2021 13

Most Popular Job Functions