Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear experts
I have the following test code:
C#
public class Person : INotifyPropertyChanged
{
    private bool? valid;

    public bool Valid
    {
        get
        {
            // Test only, expecting an exception in the next statement.
            valid = null;

            // Exception of typecast only shown while debugging *1)
            return ((bool)valid);
        }
        // ....
    }

    // ....
}
*1) typecast
Here I expect an exception and in debugger the exception will be shown.
But running the application _without_ debugger no exception will be shown, but there is one... only not shown. Btw, I do not have any try/catch which hides the exception.

This in contrast to ...
C#
private void buttonNullableBoolTest_Click(object sender, EventArgs e)
{
    bool? nullableBool = null;
    bool testBool= (bool)nullableBool; 
}
... which throws and Shows also _without_ debugger the exception "System.InvalidOperationException: Nullable object must have a value."

The question is: Why the application does not show the exception?
Any idea, what I do not understand/misinterpret this time here?
Thank you in advance.

[Edit]
Sorry, now after some more tests I see that I need to be more precise in my question.

a.) In case I access the property directly in my code e.g. if (person.Valid)... then the exception is shown also out of debugger. So here everything is like expected.

b.) But in case I bind the property to e.g. a button's Enabled property by the help of BindingSource than the app does not show the exception. It looks like .NET does hide the exception in this case :(

[Edit2]
Only for information: Binding directly in code with buttonBindingTest.DataBindings.Add(new Binding("Enabled", person, "Valid")); does also works with the expected behaviour (the app does show the exception one time).... but with the side effect that Binding will disappear :confused:

What I have tried:

Described in the question above.
Posted
Updated 22-Jul-16 3:06am
v6
Comments
Rob Philpott 22-Jul-16 6:34am    
Just a thought, but is it something to do with the thread you're on? The second example clearly happens on the gui thread, but the NotifyPropertyChange suggest that a second thread might be in play.

That's the sort of thing the debugger may be being helpful with.
[no name] 22-Jul-16 6:39am    
Thank you for your comment. Both tests running on a very simple test app, no threads nothing Special else.
BillWoodruff 22-Jul-16 8:08am    
May I suggest that sometimes it is a waste of time to try and deeply analyze how the compiler and run-time behave when you are trying to force .NET to do something that doesn't quite make sense ? Why not just make the Property Type a nullable bool and get back to work.
[no name] 24-Jul-16 2:42am    
Thank you for your feedback. You wrote "... that sometimes it is a waste of time ..."

Yes and no; why I tried to analyze this in deep?
a.) There is always a chance - at least if I write the code - that there is a bug. And in case such a bug is silently catched, it is hard to find it.
b.) I could not believe, that .NET catches silently an exception without giving me a chance to know about it.

Finally I found at MSDN, that .NET does support that I will be informed:
Event: BindingSource.DataError
Description: Occurs when a currency-related exception is silently handled by the BindingSource.
https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=DE-DE&k=k(System.Windows.Forms.BindingSource.DataError);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.5);k(DevLang-csharp)&rd=true[^]
Philippe Mori 22-Jul-16 9:19am    
I would guess that binding simply do not update value if an exception is thrown similar to what would happen if you bind to a property that do not exist.

try this

C#
bool? nullableBool = null;
       bool? testBool = (bool?)nullableBool; // validate the testBool before usage

       // or

       bool? nullableBool = null;
       bool testBool = nullableBool.HasValue ? nullableBool.Value : false;  // if null, it will be assigned as false 


for Nullable Types use HasValue Property [^]
 
Share this answer
 
v3
Comments
[no name] 22-Jul-16 6:25am    
Thank you for your reply. I know how to manage it correctly. But the question is why I don't get an exception message for the getter method when I run the app without Debugger.
Karthik_Mahalingam 22-Jul-16 6:31am    
i tried to run that without debugging in console app
i still get the exception "Nullable object must have a value."
[no name] 22-Jul-16 6:38am    
Thank you for your Feedback.
Karthik_Mahalingam 22-Jul-16 8:12am    
welcome :)
When I try it in my app built for release I do get a runtime error: "Nullable object must have a value."
C#
Person p = new Person();
Console.WriteLine(p.Valid);

So I would suspect you have a try - catch somewhere which is picking up on it.
Check your settings for debug:
"Debug" menu..."Exceptions..."
In the dialog, check the "thrown" and "user-unhandled" columns and see if there is anything you can do to get the same results as the release version.

[Update]

Sorry for the delay...I was thinking! (And that takes me some time...)
I can duplicate your problem and get an exception in VS but not when "live" very simply, without using a BindingSource, just by moving the Valid getter access to a different thread:
C#
    {
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += work_DoWork
    work.RunWorkerAsync();
    }

void work_DoWork(object sender, DoWorkEventArgs e)
    {
    Person p = new Person();
    Console.WriteLine(p.Valid);
    }


Which implies that the bound event is being executed on a different thread, which "swallows" the exception for some reason.

So... What I suspect is happening: the DataBinding is executing the code on a different thread, and when the exception is unhandled, the system shuts down the thread and doesn't notify you outside of the debugger.
A quick test:
C#
    BackgroundWorker work = new BackgroundWorker();
    work.DoWork += work_DoWork;
    work.RunWorkerAsync();
    ...
void work_DoWork(object sender, DoWorkEventArgs e)
    {
    try
        {
        Person p = new Person();
        Console.WriteLine(p.Valid);
        }
    catch (Exception ex)
        {
        File.WriteAllText(@"D:\Temp\aaaaa.txt", DateTime.Now.ToString() + " " + ex.Message);
        }
    }
And the exception is logged each end every time.

What do you do about it? Good question: I'd suggest a try-catch but exactly where you put it, I don't know...
 
Share this answer
 
v2
Comments
[no name] 22-Jul-16 6:32am    
Thank you very much for your reply. I will try your Suggestion.
BTW: This behaviour is while I'm running the Debug exe out of the debugger.
OriginalGriff 22-Jul-16 6:44am    
I get the same result with the Debug build EXE and the Release build EXE if I run them without VS loaded at all.
[no name] 22-Jul-16 6:50am    
Sorry, I was not precise enough i my question I will extend it. In case I directly access the property in a buttonclick e.g. if (person.Valid) then I get the error message. But no exception will be shown if I bind the property e.g. to a button's Enabled
OriginalGriff 22-Jul-16 7:01am    
How are you binding it?
[no name] 22-Jul-16 7:04am    
With the help of BindingSource. I just updated the question.
The BindingSource is the reason. It suppresses exceptions thrown when reading the properties. You have to subscribe to the BindingComplete event to see the errors:
How to: Handle Errors and Exceptions that Occur with Databinding[^]
 
Share this answer
 
Comments
[no name] 22-Jul-16 9:08am    
Yeah great, thank you very much. So basically my mistake using Binding witout reading the documentation :)

/[Edit]
Only for Information: This does solve "only" the Problem during binding process, but not when the exception happens after successful binding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900