views:

144

answers:

4

Possible Duplicate:
difference between throw and throw new Exception()

I'm a programmer working on adding new functionality to legacy code. While debugging, I parsed over this Catch block, which got an angry "object not set to reference of object" notice from Visual Studio:

catch(Exception ex)
            {
                SporeLog.Log("Failed to create new SavedDocumentList with Name: " + name, ex);
                throw;
            }

What does it mean to "throw". I'm familiar with throw new [exceptiontype]... but what does it mean to just... throw ?

Is this good practice, or should I alter this code to ease the trials of the developers after me?

And why does Visual Studio yell at me for doing this?

+6  A: 

It means "re-throw this same exception". In other words, continue bubbling up the exception to the next try/catch block.

It's useful if you can't actually deal with an exception at this level, but want to log that the exception happened.

Unfortunately, many people think "log & rethrow" should be done at every level, leading to applications which run slowly, have log files with every exception logged multiple times, and often never actual handle the exception.

James Curran
+3  A: 

throw; rethrows the original exception. This allows you to take some action at the current level, and then propagate the exception up the stack.

If the exception is the result of an implementation detail, it may be more appropriate to use exception-chaining to throw a domain-specific exception that wraps the original exception, to hide your clients from implementation details, and a plethora different exception types to catch.

If the exception makes sense to your clients, then rethrowing it is fine practice.

mdma
+9  A: 

Yes it is a good practice.... what it does it to re-throw the same exception that it catched, keeping the stacktrace intact

Jaime
+3  A: 

This is a good practice (sometimes). It allows you to catch an exception, log it or determine if it can be handled, then rethrow it without losing the StackTrace.

Your NullReferenceException came from SporeLog being null.

Toby
It could also be the reference to `name` that is null.
Philip Smith
@Philip, no. That would just be the same as str + "".
Matthew Flaschen
@Matthew - How do you know?? There is no info in the question as to what the type of name is. If name is a reference type and it has not been initialized it will generate the exception when accessed. If it is a value type and not initialized it would generate a different exception.
Philip Smith
@Philip, actually what I said should be true for a null reference of any type. And a default-initialized value type won't throw an exception here either.
Matthew Flaschen
@Matthew - Simply not true. If name is a null reference it does not have a ToString() method to call. Therefore it will throw an exception. There is no evidence in the question that the name variable has been initialized and if it has not it will throw an exception. So my original statement is true the exception could be caused by the name variable.
Philip Smith
@Philip, he's not calling `ToString` (*that* would cause an exception). "If an operand of string concatenation is null, an empty string is substituted." (http://msdn.microsoft.com/en-us/library/aa691375%28VS.71%29.aspx). Give me an example (for any type) of: `SomeType foo = null; "some string " + foo;` that causes an exception.
Matthew Flaschen
@Matthew - Wow, you learn something new everyday. I would have thought that this violates a founding principle of C# "Behaviour is predictable from first principles". Not throwing a exception when a null reference is accessed is unexpected. My point on ToString() is valid as your article reference confirms.
Philip Smith