throw vs. throw ex
Question
Explain the difference between
throwandthrow ex.
Short interview answer
Inside a
catch, usethrow;to rethrow while preserving the original stack trace.throw ex;resets the stack trace to the current location and makes diagnosis harder.
Detailed answer
Catch an exception only when you can handle it, add meaningful context, or translate it to an appropriate boundary-specific error:
try { SaveInvoice(); }
catch (IOException) { throw; } // Preserves SaveInvoice's stack location.
Replacing the rethrow with throw ex; would make the stack appear to begin in the catch block. Do not use exceptions as ordinary control flow when a normal branch or result can represent the expected condition.