Practice mode C#

Inside a catch block, what is the difference between throw; and throw ex;?

Correct answer B. throw; keeps the original stack trace, while throw ex; resets it to the current point

Explanation

A bare throw; rethrows the exception being handled and preserves its original stack trace, so logs still show where the error really started. throw ex; throws the same object as if it were new, replacing the stack trace with the location of the catch block and hiding the true origin. Prefer throw;, or wrap the exception in a new one with it as InnerException.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.