G
Guest
Hello, I was wondering if someone can give me a few pointers in catching an
exception correctly or at least making the code a little more elegant than
what I have. I have a rss reader that I built in VB. When I read the contents
from the xml file I create several labels controls that I place on the form
at runtime. My problem is that some rss feeds do not have a particular node
(in this case a "pubDate" for the publishing date of the feed) which causes
the application to throw a NullReferenceException. I've tried the following
code:
if nodeChannel("pubDate").InnerText = Nothing then ....
Unfortunately, this doesn't work when it comes to nulls. So, I currently
have the following code but I'm not happy with it:
Private Sub CreateControls()
title = New System.Windows.Forms.Label
rssLanguage = New System.Windows.Forms.Label
rssDescription = New System.Windows.Forms.Label
rssPubDate = New System.Windows.Forms.Label
Try
With title
.Name = "lblMyTitle"
.Text = nodeChannel("title").InnerText
.ForeColor = Color.Crimson
.SetBounds(47, 16, 53, 13)
End With
With rssLanguage
.Name = "lblRssLanguage"
.Text = nodeChannel("language").InnerText
.ForeColor = Color.Crimson
.SetBounds(194, 16, 39, 13)
End With
With rssDescription
.Name = "lblRssDescription"
.Text = nodeChannel("description").InnerText
.ForeColor = Color.Crimson
.SetBounds(75, 55, 500, 13)
End With
With rssPubDate
.Name = "lblPubDate"
.Text = nodeChannel("pubDate").InnerText
.ForeColor = Color.Crimson
.SetBounds(329, 16, 200, 13)
End With
Catch e As NullReferenceException
With rssPubDate
.Name = "lblPubDate"
.Text = "No publish date!"
.ForeColor = Color.Crimson
.SetBounds(329, 16, 200, 13)
End With
Finally
Dim linkFeed As New linkCreated(AddressOf CreateLink)
Me.Invoke(linkFeed, title, rssLanguage, rssDescription,
rssPubDate)
End Try
End Sub
As you can see when the exception occurs I'm assuming there's no pubDate in
the rss feed so I create a label saying No Publish Date. Is there a better
way in handling this. If so, please share you thoughts and ideas. I would
love to learn your methodology when you encounter issues such as mine. Thanks.
exception correctly or at least making the code a little more elegant than
what I have. I have a rss reader that I built in VB. When I read the contents
from the xml file I create several labels controls that I place on the form
at runtime. My problem is that some rss feeds do not have a particular node
(in this case a "pubDate" for the publishing date of the feed) which causes
the application to throw a NullReferenceException. I've tried the following
code:
if nodeChannel("pubDate").InnerText = Nothing then ....
Unfortunately, this doesn't work when it comes to nulls. So, I currently
have the following code but I'm not happy with it:
Private Sub CreateControls()
title = New System.Windows.Forms.Label
rssLanguage = New System.Windows.Forms.Label
rssDescription = New System.Windows.Forms.Label
rssPubDate = New System.Windows.Forms.Label
Try
With title
.Name = "lblMyTitle"
.Text = nodeChannel("title").InnerText
.ForeColor = Color.Crimson
.SetBounds(47, 16, 53, 13)
End With
With rssLanguage
.Name = "lblRssLanguage"
.Text = nodeChannel("language").InnerText
.ForeColor = Color.Crimson
.SetBounds(194, 16, 39, 13)
End With
With rssDescription
.Name = "lblRssDescription"
.Text = nodeChannel("description").InnerText
.ForeColor = Color.Crimson
.SetBounds(75, 55, 500, 13)
End With
With rssPubDate
.Name = "lblPubDate"
.Text = nodeChannel("pubDate").InnerText
.ForeColor = Color.Crimson
.SetBounds(329, 16, 200, 13)
End With
Catch e As NullReferenceException
With rssPubDate
.Name = "lblPubDate"
.Text = "No publish date!"
.ForeColor = Color.Crimson
.SetBounds(329, 16, 200, 13)
End With
Finally
Dim linkFeed As New linkCreated(AddressOf CreateLink)
Me.Invoke(linkFeed, title, rssLanguage, rssDescription,
rssPubDate)
End Try
End Sub
As you can see when the exception occurs I'm assuming there's no pubDate in
the rss feed so I create a label saying No Publish Date. Is there a better
way in handling this. If so, please share you thoughts and ideas. I would
love to learn your methodology when you encounter issues such as mine. Thanks.