Reputation: 625
I have the following code:
public static Point operator /(Point point, double value)
{
if (value == 0)
{
throw new DivideByZeroException("Cannot divide by zero");
return Point.Origin;
}
return new Point(point.X / value, point.Y / value, point.Z / value);
}
And the first return statement (return Point.Origin;
) is underlined in green in Visual Studio. The message says "Unreachable code detected" when the cursor is hovered over the underlined text. This leads me to my question stated in the title line:
Does throwing an exception within a method cause the method to return?
Upvotes: 9
Views: 8113
Reputation: 23646
Does throwing an exception within a method cause the method to return?
Yes. Execution of a method is being interrupted, so method returns it execution flow. But no value is returned you defined it with return
statement. No line after throw new
in current method is being executed, except of corresponding catch
and finally
blocks. So you don't have to return values after throwing an exception.
Visual Studio underlines blocks of code, which it detected cannot be reached. In your specific case return Point.Origin;
statement is never going to be executed, so you can remove it.
Upvotes: 1
Reputation: 2786
No, because throwing an exception is not a return.
If the exception is not handled inside the function it will cause an immediate exit out of the function, passing control to the first point in the program where the exception will be catched ie handled. (Resulting in a sudden death of the program if it doesn't get handled.)
Since the function will exit after the throw the return statement is useless as it will never be reached.,
Upvotes: 3
Reputation: 13064
Not exactly. When an exception is thrown, if caught, it will jump to the handling code within the method. From there it can do whatever you'd like. If uncaught as in your example, it will jump to the method that called this method and will never "return" a value. It will also never return to execute the return line you have after your thrown exception.
Upvotes: 2
Reputation: 1502696
Well, it causes the execution of the method to exit, yes. The exception is thrown up the stack, to the closest method which catches it. If it didn't affect execution flow, it would be pretty pointless.
This isn't the same as the method returning "normally" - i.e. without an exception. So suppose the calling method had:
Point foo = bar / baz;
Console.WriteLine("Done");
In this case the Console.WriteLine()
call wouldn't execute if the division operator threw an exception. Instead, either the exception would be caught in this method, or the exception would propagate to that method's caller, etc.
(finally
blocks will be executed along the way too.)
It's probably worth reading the MSDN guide to "handling and throwing exceptions".
Upvotes: 11
Reputation: 67283
Yes, throwing an exception causes the method to return immediately.
However, it's not a normal return. For example, there is no return value. The only way to capture control from a method that is returning as a result of an exception being thrown, is to have an exception handler.
Upvotes: 0