6
When I test my application, it returns some specific data, but when the requested data is null, this untreated exception error appears
I tried to capture him but I think I’m making a mistake.
How can I leave a treatment that warns in a textbox that the requested data was not found?
'Buscar informações via GoogleBook API (precisa ser arrumado)'
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Step 1:- google API url responsible for returning the book detail in JASON format'
Const GOOGLEAPIURL As String = "https://www.googleapis.com/books/v1/volumes?q="
If Me.txtISBN.Text <> String.Empty Then
Dim requestURL As String
'Step 2:- Reformed the URL to target particular ISBN number'
requestURL = GOOGLEAPIURL + Me.txtISBN.Text.Trim() + "+isbn"
'Step 3: created Http webrequest for URL'
Dim wr As HttpWebRequest = HttpWebRequest.Create(requestURL)
'Step4 : get the response of web request in http web response object'
Dim resp As HttpWebResponse = wr.GetResponse()
'Step 5: passes the response stream in stream reader'
Dim sreader As New StreamReader(resp.GetResponseStream())
'Step 6: parsing the reader(which is in Jason format) using JASON.NET'
Dim rss = JObject.Parse(sreader.ReadToEnd())
'Step 7: if object find the fetch the detail'
If rss Is Nothing = False AndAlso rss.Count > 0 Then
Me.lblBookName.Text = rss.Item("items")(0).Item("volumeInfo").Item("title").ToString()
Me.lblSubtitle.Text = rss.Item("items")(0).Item("volumeInfo").Item("subtitle").ToString()
Me.lblAuthor.Text = rss.Item("items")(0).Item("volumeInfo").Item("authors").ToString().Replace("[", "").Replace("]", "").ToString()
Me.lblPublisher.Text = rss.Item("items")(0).Item("volumeInfo").Item("publisher").ToString()
Me.lblPublishedDate.Text = rss.Item("items")(0).Item("volumeInfo").Item("publishedDate").ToString()
Me.lblPageCount.Text = rss.Item("items")(0).Item("volumeInfo").Item("pageCount").ToString()
'Captura de erro'
Try
rss = True
Catch ex As Exception
End Try
Else
MsgBox("Nada aqui")
End If
Else
Me.lblMessage.Text = "Please enter the ISBN number"
End If
So my intention was to capture the exception and continue running the Try block to get the remaining data, I know this is not good practice, but I would have plausible reasons to explain the pq....
– Everton Souza Santos
If you had, you would have explained. This makes no sense under any circumstances that I know. The
try-catch
that you used is probably the strangest thing I’ve ever seen.– Maniero
would you have explained it here in the question or commented on the code? Anyway... it’s the method I found to not stop the execution of the program, I put only one Msgbox warning about data that was not found. So I came to ask about what other method you could be using... :/
– Everton Souza Santos
I don’t think you read my answer. The program is stopping for a programming error. You have to fix it and not let it stop. You want to throw the dirt under the carpet, this does not solve anything. The method is to check (`if``) if the information is present. If it is not, you inform the user of this situation, but do not let the program generate an exception. If you want to learn about exceptions, I wrote a lot about this: http://answall.com/search?q=user%3A101+%5Bexception%5D
– Maniero
I got it, I got it wrong, but thanks anyway. I was able to solve it with the adapted excerpt @jbueno gave me, and I understood now what you meant about playing under the rsrs . Vlw itself ;)
– Everton Souza Santos