What is the difference between
try { ... }
catch{ throw }
and
try{ ... }
catch(Exception e) {throw new Exception(e.message) }
regardless that the second shows a message.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
throw;rethrows the original exception and preserves its original stack trace.throw ex;throws the original exception but resets the stack trace, destroying all stack trace information until yourcatchblock.NEVER write
throw ex;throw new Exception(ex.Message);is even worse. It creates a brand newExceptioninstance, losing the original stack trace of the exception, as well as its type. (eg,IOException).In addition, some exceptions hold additional information (eg,
ArgumentException.ParamName).throw new Exception(ex.Message);will destroy this information too.In certain cases, you may want to wrap all exceptions in a custom exception object, so that you can provide additional information about what the code was doing when the exception was thrown.
To do this, define a new class that inherits
Exception, add all four exception constructors, and optionally an additional constructor that takes anInnerExceptionas well as additional information, and throw your new exception class, passingexas theInnerExceptionparameter. By passing the originalInnerException, you preserve all of the original exception’s properties, including the stack trace.