In the class:
private Func<T, object> pony;
In my function:
object newValue;
try {
newValue = pony.Invoke(model as T); // This is the line where I get an exception!
} catch (Exception exception) {
// This code is never run, even though I get an exception two lines up!
if(exception is DivideByZeroException) throw new DivideByZeroException("Division by zero when calculating member " + GetMemberName(), exception);
throw;
}
I expect to get exceptions when I throw them, but I get a DivideByZeroException on the line newValue = pony.Invoke(model as T);. Why is this? Can I do something about it?
This is in a asp.net mvc2-application running in Cassini at the moment.
If I select Start debugging in Visual Studio 2008, the error gets caught and rethrown with the extra information!
The problem was that I obviously haven’t understood how inner exceptions work. The exception gets caught but then only the inner exception is shown, and that’s a totally other issue.
Exceptions thrown from a compiled expression are handled normally by the
try .. catchconstruct, so I’d expect that there is some other issue in your code. If you try for example the following code, it behaves as expected:As a side note, you should probably handle
DivideByZeroExceptionusing a separatecatch(as I did in my example). This is a cleaner and recommended way to catch different types of exceptions.Can you check whether the exception is really unhandled when running the application without debugging (for example by adding some debug print to the
catchblock)? What exception is printed when you run the application (afterall, your code rethrows some exception in any case, so the output may not be clear).