PC Review


Reply
Thread Tools Rate Thread

ApplicationException -> MissingMethodException

 
 
=?Utf-8?B?TmlsbGU=?=
Guest
Posts: n/a
 
      5th Dec 2004
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
 
Reply With Quote
 
 
 
 
Daniel Moth
Guest
Posts: n/a
 
      5th Dec 2004
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
--
http://www.danielmoth.com/Blog/


"Nille" <(E-Mail Removed)> wrote in message
news:11F9C117-C0A2-489B-83FD-(E-Mail Removed)...
> 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



 
Reply With Quote
 
Stuart Celarier
Guest
Posts: n/a
 
      5th Dec 2004
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...stemmissingmet
hodexceptionclasstopic.asp


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: ApplicationException unhandled by user code bg_ie@yahoo.com Microsoft C# .NET 0 24th Apr 2007 02:19 PM
ApplicationException unhandled by user code bg_ie@yahoo.com Microsoft C# .NET 1 24th Apr 2007 01:52 PM
Need help with ApplicationException =?Utf-8?B?Q2hhcmxlcw==?= Microsoft ASP .NET 2 11th Aug 2005 07:42 PM
ApplicationException OpticTygre Microsoft VB .NET 3 17th May 2005 12:40 PM
My application exits after handling an ApplicationException Tad Marshall Microsoft VB .NET 4 11th Apr 2005 01:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:40 PM.