How solve this IF Then problem?

  • Thread starter Thread starter Shapper
  • Start date Start date
S

Shapper

Hello,

I need to load a XML file and call a function if it wasn't possible.
I have this:

Sub Build_RSS()

Dim news As New XmlDocument()
If news.Load("http://www.domain.com/news.rss") Then
' Do Actions
Else
Display_Error("File not Found")
End If

End Sub

However the page gets an error when the URL of the file is wrong instead
of displaying my custom error. How can I solve this?

Thanks,
Miguel
 
Hi,

If you look at the documentation for then XmlDocument.Load() method you will
notice that it is a Sub and does not return a value. If there are any errors
it will throw an exception. Rather than using an if statement you should
catch the exceptions thrown by the method and deal with the exception
appropriately.

Hope this helps
 
Hi,

You were right. I did as follows:
Sub Build_RSS()
Try
news.Load("http://www.domain.com/news.rss")
Catch ex As Exception
Display_Error("File not Found")
End Try
End Sub

It works fine. There is only one problem: I have the code to build the
news system after this. How can I interrupt Build_RSS() after calling
Display_Error?

Thank You,
Miguel
 
Hi,

Sub Build_RSS()
Try
news.Load("http://www.domain.com/news.rss")
' Do the work here ... if the Load thows an exception then this is
skipped
Catch ex As Exception
Display_Error("File not Found")
End Try
End Sub

Note that you should be more specific about the exception, an exception
could be raised for a number
of reasons other than file not being found.
 
Back
Top