views:

69

answers:

2

I'm creating a parsing application, which parses ~20 sites, ~7-15 values from each. Pseudocode looks like:

ParserA : ParserBase 
{
public override SomeEntity Parse(...)
{
 SomeEntity se = new SomeEntity();

 //some code, parsing value1;
 //some code, parsing value1;
 //some code, parsing value1;

 //some code, parsing value2;
 //some code, parsing value2;
 //some code, parsing value2;

 //some code, parsing value3;
 //some code, parsing value3;
 //some code, parsing value3;

 //some code, parsing value4;
 //some code, parsing value4;
 //some code, parsing value4;

 ...

 return se; 
}
}

ParserB : ParserBase {...} 
ParserC : ParserBase {...} 
...

etc.

As soon as parsers have never done well with html(layouts happen to change during time), I need to implement exceptionHandling and logging. I need to parse as much as possible, and errors must be logged. I know 2 ways to deal with it:

public override SomeEntity Parse(...)
{
 SomeEntity se = new SomeEntity();

try {
 //some code, parsing value1;
 //some code, parsing value1;
 //some code, parsing value1;

 //some code, parsing value2;
 //some code, parsing value2;
 //some code, parsing value2;

 //some code, parsing value3;
 //some code, parsing value3;
 //some code, parsing value3;

 //some code, parsing value4;
 //some code, parsing value4;
 //some code, parsing value4;

 ...
}
catch (Exception e)
{
 //Log
}
 return se; 
}

Pros: Easy to implement

Cons: if i get exc at value5, i have no chance to parse value6,7,.. etc.

2)

ParserA : ParserBase 
{
public override SomeEntity Parse(...)
{
try
{
 //some code, parsing value1;
 //some code, parsing value1;
 //some code, parsing value1;
}
catch(Exception e)
{
 // Log
}

try
{
 //some code, parsing value2;
 //some code, parsing value2;
 //some code, parsing value2;
catch(Exception e)
{
 // Log
}

try
{
 //some code, parsing value3;
 //some code, parsing value3;
 //some code, parsing value3;
catch(Exception e)
{
 // Log
}

try
{
 //some code, parsing value4;
 //some code, parsing value4;
 //some code, parsing value4;
catch(Exception e)
{
 // Log
}

 ...

}
}

Pros: Everything, that can be parsed, is parsed;

Cons: Too much copypaste (remember 20 parsers, 7-15 values in each.

I want to write less, do more, so i've implemented Safecall function which takes delegate and executes it inside a try-catch block, and logs ot. So now i have to write this:

SafeCall( () => { 
 //some code, parsing value4;
 //some code, parsing value4;
 //some code, parsing value4;
});

instead of this:

try
{
 //some code, parsing value4;
 //some code, parsing value4;
 //some code, parsing value4;
catch(Exception e)
{
 // Log
}

Is this a good solution or i'm reinventing a square wheel?

+2  A: 

Go with the SafeCall option as it works, is easy to read and you can always change the SafeCall implementation if you want to change the logging mechanism.

Phil Wright
+1  A: 

I would say defensive coding in XP terms would be 'the' solution in your case .

  • That is checking for UIElement != null before parsing any values from the expeceted UI element. Because the user is tend to change the HTML markup. (I have experienced this in my screen scrapping application)

  • This way you don't have to use multiple try catch blocks to parse different values.

  • You can simply load the DOM and traverse along the interested node(UIElement) and parse only the non - null elements .

Please refer the Best practices for Exception Handling from Microsoft.

I take that you just want to skip the nodes, that were not found, from parsing.

Hope this helps,

Thanks,

Vijay

vijaysylvester
Yes, i want to skip the nodes that cant be parsed(sort of). I do not want to use null checks because usually i have to check this : XpathQuery Result, Regex Result, Collection Length, and sometimes - all of them together.
portland