Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catch exception, validate input or both?

I'm working on obtaining RSS feeds like so:

SyndicationFeed rss = SyndicationFeed.Load(XmlReader.Create(textBox1.Text));

XmlReader.Create() in this case can throw up to 4 exceptions related to things like the parameter being null, 404 error, etc.

Should I try to validate the Uri (make sure it's not null, 404, correct doctype, etc) before I call that line or should I just handle the exceptions? I know I've read numerous times on SO that exceptions should be used for truly exceptional circumstances and I agree this doesn't seem to meet that prerequisite but it seems easier to just handle the exceptions.

like image 994
Pete Avatar asked Oct 11 '22 20:10

Pete


1 Answers

The Text property of a checkbox is never null, so you can skip that one. Check manually all the cases that are simple and not related to the process like string being empty and Url being correct and leave more obscure XML-specific stuff to the validation inside Create method. So the answer is to combine both.

Also, let me once again refer to a blog post by Eric Lippert about exceptions. In your case both vexing and exogenous exceptions can happen, so you should probably catch them. But make sure that boneheaded ones don't occur.

like image 103
Dyppl Avatar answered Nov 13 '22 02:11

Dyppl