ApplicationException -> MissingMethodException

G

Guest

Hi! I have an odd problem with an exception that doesn't arrive properly. I
have a situation similiar to this:

class MyClass
{
public Object MyProperty
{
get
{
return myProperty;
}

set
{
Object oldValue = myProperty;
myProperty = value;
if(OnPropertyChanged != null)
{
try
{
OnPropertyChanged(...);
} catch (Exception)
{
myProperty = oldValue;
throw;
}
}
}
}
}

In the invokation list of OnPropertyChanged I (intentionally; invalid
property value) throw a custom exception (derived from ApplicationException),
but the exception I catch above is a MissingMethodException! My exception
just vanishes into thin air and gets replaced by that exception. This is not
good, since my exception contains information that I need to present to the
user.

And what is ever stranger is that the value of MyProperty will change to the
new value, even though I restore that value (myProperty = oldValue).

What am I doing wrong? I'm looking at the code being executed (debugger) and
can actually see that the line "myProperty = oldValue" executes properly. Yet
the value of the property will be the value that caused the exception. The
property is not changed elsewhere either.

I haven't been using C# very long, so I don't know the mechanics of the
language. For instance, can objects be copied? This would still not explain
the exception, but at least it would explain why the property doesn't get
restored.

Thanks in advance,
Nille
 
D

Daniel Moth

On the face of it I see no problem. Show us the relevant rest of the class
as well as the client code.

Cheers
Daniel
 
S

Stuart Celarier

The MissingMethodException "is thrown when there is an attempt to
dynamically access a method that does not exist." [1] The compiler
reports an error if code tries to access a nonexistent method on a
class. This exception suggests that you are running afoul when invoking
a method that is dynamically identified.
My exception just vanishes into thin air and gets replaced by that
exception.

I suspect that you are assessing the situation incorrectly. More likely,
the MissingMethodException is being thrown before any of your code could
generate a custom exception.

You showed us

if(OnPropertyChanged != null)
{
try
{
OnPropertyChanged(...);
}
...
}

How is OnPropertyChanged declared? How is it initialized? In particular,
is every non-null value of OnPropertyChanged absolutely guaranteed to be
a method that can be invoked as shown? Do the signatures (argument and
return types) match identically? Is there any method overloading
involved in this code?

Please show us a complete, minimal example that illustrates the problem.


As a separate issue, you should reexamine your exception handling logic.
my [custom] exception contains information that I need to present to
the user

If that is the case, then you should catch the type of your exception
like this:

class InvalidPropertyChangeException : Exception { /* ... */ }

try
{
// ...
}
catch ( InvalidPropertyChangeException ex )
{
// access the custom exception
// process exception, rethrow if necessary
}
catch ( Exception ex ) // unexpected exception
{
// process exception, rethrow if necessary
}

Cheers,
Stuart Celarier, Fern Creek

[1]
http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemmissingmet
hodexceptionclasstopic.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top