When we use throw ex, it throws a new exception with empty call stack or call stack starting from there to the higher\caller. It's always a good habit to use 'throw' instead on 'throw ex' inside catch block since it retains the complete call stack for the exception. 'Throw ex' can be replaced with 'throw new exception' to add mode details to the exception caught or add custom information to the exception. Throwing new exception is preferred in cases where code block raises an exception for cases like invalid input or format etc and add explanatory message about that case to that exception.
Choosing between throw and throw new exception("message") or throw ex mainly depends on ,
a. What 'call stack' you like to retain or send to the caller.
b. What information you like the caller to see on catching this exception like custom message for that exception or application specific information\exception types.
No comments:
Post a Comment