Monday, November 21, 2005

re-throwing an exception with a paramenter it resets StackTrace

How about Throw without the ex

The Throw statement without an agrument rethrows the current exception object and must appear in a Catch clause to be valid.

If you throw an exception with a paramenter it resets StackTrace property of the exception object (as if a brand-new exeption were created).

The version without an argument is perferable for rethrowing the same exception becuase it lets the caller routine determine exactly where the exception occured.

Additional Notes:
- Throw an InvalidOperationException if a property or method call occurs when the object is in a state that can't handle the method call
- Throw and ArgumentException (or a class that inherits for the AgrumentException) if a bad argument is passed.
- Stick to predefined exceptions, and don't define custom exceptions unless strictly required.
- Don't throw expetions for relatively common errors, such as end-of -file or timeout; instead return a special value to the caller.

No comments: