Is there any way to direct C# to ignore NullReferenceException (or any specific exception for that matter) for a set of statements?
This is useful when trying to read properties from a deserialized object that may contain many null objects in it.
Having a helper method to check for null could be one way but I’m looking for something close to ‘On Error Resume Next’ (from VB) at a block of statement level.
Try-Catch will skip the succeeding statements on exception
try
{
stmt 1;// NullReferenceException here, will jump to catch - skipping stmt2 and stmt 3
stmt 2;
stmt 3;
}
catch (NullReferenceException) { }
For Example: I’m deserializing an XML message to an object and then try to access a property like
Message.instance[0].prop1.prop2.ID
now prop2 could be a null object (because it doesn’t exists in XML Message – an optional element in XSD). right now I need to check for null for each element in the hierarchy before accessing the leaf element. i.e I’ve to check if instance[0], prop1, prop2 are not null, before accessing ‘ID’.
any better way that avoids null-checking for each element in the hierarchy?
now I’m using delegate and NullReferenceException handling
public delegate string SD();//declare before class definition
string X = GetValue(() => Message.instance[0].prop1.prop2.ID); //usage
//GetValue defintion
private string GetValue(SD d){
try
{
return d();
}
catch (NullReferenceException) {
return "";
}
}
"One who imagines himself to be all-knowing will surely suffer on account of his ignorance." Hazrat Ali (God be please with Him)
Subscribe to:
Post Comments (Atom)
Popular Posts
-
IEnumerable (T) Interface Exposes the enumerator, which supports a simple iteration over a collection of a specified type. Namespace: S...
-
1. Introduction I have noticed an increase in the number of articles published in the Architect category in code-project during the last few...
-
Introduction Microsoft SQL Server 2005 introduces a new distributed messaging framework that allows for asynchronous programming support. Th...
-
What is Selenium? Selenium automates browsers. That's it! What you do with that power is entirely up to you. Primarily, it is for au...
-
Start day with the name of Allah Wake early in the morning at 8 o Clock Brush your teeth and take bath Prepare breakfast Leave for off...
-
Reference: Adam McKerlie in Guides, Programming If there is one thing that a programmer is constantly doing throughout their entire careers...
-
Problem: I've installed a SQL Reporting server 2012, with some reports. But I've some performances issues. The first ca...
-
Microsoft.be has published my ORDINA chopsticks session. In this presentation I guide you through the use of the Web Service Software Factor...
-
This is a list of questions I have gathered from other sources and created myself over a period of time from my experience, many of which I ...
-
[ Authorize ] public class ImportDataController : Controller { SMSNotificationService smsNotification = new SMS...
No comments:
Post a Comment