1. Why would the problem line in the StackTrace of this exception point to the Throw statement and NOT to the line f.Delete()?
It depends which stack trace you're looking at: that generated by the rethrow (the Throw
inside your catch block), or that by first exception being caught?
Set a breakpoint on the Catch ex As Exception
line and inspect ex
with the debugger; you will see a stack trace pointing to f.Delete()
.
Throw
means "re-throw", i.e. the original stack trace will be preserved... However, you don't get two entries in the stack trace for the same method invocation: if the exception gets re-thrown in the same method that originally caused the exception, the stack trace will only contain an entry pointing to the final re-throw.
2. How can I keep the Try/Catch but have the CORRECT stack trace pointing to f.Delete() as the source of the problem?
Remember that a re-throw basically means, "I was wrong, I don't know how to handle this exception after all." So instead of re-throwing, you might be able to use an exception filter (Catch … When <boolean-expression>
) to decide whether you actually can handle the exception before you catch it. If you can determine that before catching it, a rethrow should become unnecessary, and the stack trace will not get modified.
Put the f.Delete()
in a different method. That way, it gets its own stack frame / line in the stack trace and won't get overwritten by a re-Throw
.
(Or, simply don't catch the exception just to rethrow it.)