How solve this IF Then problem?

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
 
C

Chris Taylor

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
 
S

Shapper

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
 
C

Chris Taylor

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.
 
G

Guest

Hello
In your case it is better to use:
Catch ex As FileNotFoundException
Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top