JenniferLostTheWar
New member
- Joined
- Dec 22, 2015
- Messages
- 1
- Programming Experience
- Beginner
Hello I?m very new to programming and I am writing a read only address book using the json.net library. Each person in the address book has about 30 values. However sometimes a person may be missing a couple of values.
When the value is missing the program locks and then crashes. So I implemented a try/catch around each value. However I get a feeling that isn?t the right way to approach this.
What would be the optimal way of approaching this? I thought about writing a function that will intake the values and then output the result. This way I would only have to do a try/catch 1 time in side that function.
Just don?t want to learn any bad habits.
When the value is missing the program locks and then crashes. So I implemented a try/catch around each value. However I get a feeling that isn?t the right way to approach this.
What would be the optimal way of approaching this? I thought about writing a function that will intake the values and then output the result. This way I would only have to do a try/catch 1 time in side that function.
Just don?t want to learn any bad habits.
C#:
JObject jsonObj = JObject.Parse(jsonData);
try
{
key1 = jsonObj["data"]["name"].ToString();
}
catch
{
key1 = "Not There";
}
try
{
key2 = jsonObj["data"]["phone"].ToString();
}
catch
{
key2 = "Not There";
}
try
{
key3 = jsonObj["data"]["address"]["city"].ToString();
}
catch
{
key2 = "Not There";
}