Preventing This Code From Crashing

Hi,

I have an app that uses the Yahoo’s WOEID weather information from the Internet. The app regularly updates the weather information using a timer. If the weather server is down or does not load correctly my app crashes. I was wondering how I could trap this error so the app keeps going even if the weather information is not available when it is requested. The code it crashes on is listed below.

[code] // This returns an RSS feed, which is technially XML.
// So the XML can be loaded and parsed to get the weather information.

Dim weatherXml As New XmlDocument
Try
weatherXml.LoadXml(weatherInfo)
Catch e As XmlException
MsgBox("XML error: " + e.Message)
End Try[/code]

Any help would be greatly appreciated.

Jim

What do you mean by “crashes”? This code should correctly catch the error and allow your app to continue.

Keep in mind that if you are running from Xojo in debug mode, the debugger will still stop on the exception if Project->Break on Exceptions is checked. This is not a crash, though, and would not cause your built app to stop.

[quote=131202:@James Redway]Hi,

I have an app that uses the Yahoo’s WOEID weather information from the Internet. The app regularly updates the weather information using a timer. If the weather server is down or does not load correctly my app crashes. I was wondering how I could trap this error so the app keeps going even if the weather information is not available when it is requested. The code it crashes on is listed below.

[code] // This returns an RSS feed, which is technially XML.
// So the XML can be loaded and parsed to get the weather information.

Dim weatherXml As New XmlDocument
Try
weatherXml.LoadXml(weatherInfo)
Catch e As XmlException
MsgBox("XML error: " + e.Message)
End Try[/code]

Any help would be greatly appreciated.

Jim[/quote]

If it is anything like the code posted a few months ago at https://forum.xojo.com/6087-displaying-weather-in-app ? I would rather look into the hsock HTTPSocket for possible errors when the server is down.

When you say “crash”, do you mean the app freezes, silently closes, or reports an error ? In case of hard crash, I am sure a carsh dump would help.

Hi Paul and Michael thanks for taking the time to help me.

When running the app in Xojo (debugger), the app creates an exception and displays the code I showed above. It no longer will run. I have to stop the app and restart it in Xojo.

I did have Project → Break On Exception checked. That must be it. Thanks for your help. I will try that. I appreciate it.

That was the same code. I posted the original thread.

Thanks again for your help. :slight_smile:

You may want to check what weatherInfo contains. If HTTPSocket cannot connect to the server, chances are it is empty which triggers the exception. Then all you got to do to fix the issue is to place the code you posted in an

if weatherInfo <> "" then end if

Can you clarify this? Are you saying you cannot click the “Resume” button and then click OK in your MsgBox?

Now what happens with Break On Exceptions off, I get a message after I click the message box that the application quit unexpectedly, so it still closes down the app even with the error handling. So I don’t know what to do about that.

Below is the entire section of code.

[code]Sub GetWeather()

//Makes sure computer is connected with the Internet to get Weather.
if System.Network.IsConnected then
//MsgBox “you’re online!”
else
Exit
end if

// The WOEDI is the Yahoo weather location ID.
// You can get this by visiting http://weather.yahoo.com
// and entering a ZIP code.
// The WOEID appears in the resulting URL as the numbers at the end:
// http://weather.yahoo.com/united-states/maine/south-portland-12759449/

Dim url As String = “http://weather.yahooapis.com/forecastrss?w=” + txtWOEID.Text
Dim socket As New HttpSocket
Dim weatherInfo As String

weatherInfo = socket.Get(url, 5)
weatherInfo = DefineEncoding(weatherInfo, Encodings.UTF8)

WeatherInfoArea.Text = weatherInfo

// This returns an RSS feed, which is technially XML.
// So the XML can be loaded and parsed to get the weather information.

Dim weatherXml As New XmlDocument

Try
weatherXml.LoadXml(weatherInfo)
Catch e As XmlException
Exit
MsgBox("XML error: " + e.Message)
End Try

// Extract the current conditions from the XML
Dim itemNodes As XmlNodeList
itemNodes = weatherXml.Xql(“//item”)

Dim itemNode As XmlNode
For i As Integer = 0 To itemNodes.Length-1
itemNode = itemNodes.Item(i)

Dim child As XmlNode
For c As Integer = 0 To itemNode.ChildCount-1
  child = itemNode.Child(c)
  Select Case child.Name
  Case "title"
    txtTitle.Text = child.FirstChild.Value
    
  Case "yweather:condition"
    
    txtConditions.Text = child.GetAttribute("text")
    txtWDate.Text = child.GetAttribute("date")
    txtTemp.Text = child.GetAttribute("temp")

End Select
Next
Next

CallChannel[/code]

So if I do this it may prevent the error?

[code] weatherInfo = socket.Get(url, 5)

weatherInfo = DefineEncoding(weatherInfo, Encodings.UTF8)'=

If weatherInfo < > “” Then

WeatherInfoArea.Text = weatherInfo
// The rest of the code below.

End If[/code]

Your code flow looks problematic. Have you stepped through it in the debugger? Why would you want your code to continue and “Extract the current conditions from the XML” after you catch an exception with the XML?

I don’t know. I guess I was just following the advice I received when I posted my original message at this link:

link text

[quote]Why would you want your code to continue and “Extract the current conditions from the XML” after you catch an exception with the XML?
[/quote]

In looking at it, I thought the Exit command would send me out of the method if it caught the exception. Therefore it would stop the whole process.

Try weatherXml.LoadXml(weatherInfo) Catch e As XmlException Exit MsgBox("XML error: " + e.Message) End Try

What version of Xojo are you using? I’ve experienced untrappable exceptions with xml parsing in the past but I think they’ve been fixed.

Looks like your code will have a nil object exception but it shouldn’t ‘quit unexpectedly’.

Hi Will,

I am using Xojo 2014 Release 2.1

Dunno but I just loaded your code into the IDE and ran it (after adding the required text fields etc)
Runs fine here

Hmm. I didn’t notice that, but if your MsgBox is displaying, it would indicate that you are not in fact exiting the method. I typically use “Return” rather than Exit, but that should not matter. Against, stepping through the code in the debugger should tell you exactly what lines are executing and what is causing the exception.

ok, it’s been well fixed what I thought it might be.

I can’t reproduce an unexpected quit, new XmlDocument with a garbage string or “” triggers the exception and then continues fine. I guess if you can identify when it’s going to choke just don’t try. Or a brute force way to pin down which line execution terminates at is to litter debug logs everywhere…

System.DebugLog("0") Try System.DebugLog("1") weatherXml.LoadXml(weatherInfo) System.DebugLog("2") Catch e As XmlException System.DebugLog("3") Exit ...

[quote=131220:@James Redway]So if I do this it may prevent the error?

weatherInfo = socket.Get(url, 5)

weatherInfo = DefineEncoding(weatherInfo, Encodings.UTF8)’=

If weatherInfo < > “” Then

WeatherInfoArea.Text = weatherInfo
// The rest of the code below.

End If[/quote]

If what I suspect is happening, the error should go away. Just try it.