954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Exception Handling

I'm curious about something.

If I have a listbox that is databound and a filter is applied to that listbox based on the value in a combobox, what would be the best way to handle the null reference exception thrown when the form loads or the filter changes and everything is filtered out. Right now I just have a "IsLoading" and "IsClosing" bool value that I change in those two events and just have a simple if statement to basically ignore the exception if either of those values is true.

But I'm not sure if this is the best way to handle this. I keep thinking that if a method is throwing an exception and you just "filter" the exceptions like I am doing and ignore some, it could be a problem later.

Any suggestions/thoughts?

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

Can you test the contents first to make sure you're not going to get an Exception?
If you're using Linq, can you set your "where" clause to only get data that exists in the master list?

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

I'm not using Linq, I can't stand it, but I could check for the data first, I'm just worried about speed with something like that. I'll check that out! Thanks for the suggestion!

zachattack05
Posting Pro
516 posts since Dec 2009
Reputation Points: 61
Solved Threads: 15
 

If it never changes or it is small, it's not a problem.
Handling an exception in a loop might take more time.

thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You