Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - Aiuto - Catch non intercetta Exception in funzione Async
Forum - C# / VB.NET - Aiuto - Catch non intercetta Exception in funzione Async

Avatar
Antonio4910 (Normal User)
Newbie


Messaggi: 1
Iscritto: 06/12/2023

Segnala al moderatore
Postato alle 19:12
Mercoledì, 06/12/2023
Quando eseguo il codice mi vengono segnalate in Output segnalazioni di errore (IOException e HTTPrequestException) che però non riesco ad intercettare. QUalcuno a qualche suggerimento? Grazie
    Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of (String, String))
        Dim maxRetries As Integer = 3  ' Numero massimo di tentativi
        For attempt As Integer = 1 To maxRetries
            Try
                Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
                Using content As HttpContent = response.Content
                    Dim urlContents As String = Await content.ReadAsStringAsync(ct)
                    Return (url, urlContents)
                End Using
            Catch ex As HttpRequestException
                If attempt < maxRetries Then
                    Task.Delay(1000).Wait()     ' Ritenta dopo un secondo di attesa
                Else
                    Throw New Exception("Errore di I/O dopo i tentativi di ritentativo.")
                End If
            Catch ioEx As IOException
                If attempt < maxRetries Then
                    Task.Delay(1000).Wait()      ' Ritenta dopo un secondo di attesa
                Else
                    Throw New Exception("Errore di I/O dopo i tentativi di ritentativo.")
                End If
            Catch ex As Exception
                MessageBox.Show("Errore generale: " & ex.Message)
            End Try
        Next
    End Function

PM Quote
Avatar
Carlo (Member)
Guru


Messaggi: 1364
Iscritto: 29/01/2018

Segnala al moderatore
Postato alle 21:20
Mercoledì, 06/12/2023
Modifica la domanda con [ edit ] e per il codice usa il tastino [ Code ], si trova a sinistra della finestra dove scrivi il messaggio.

Il codice verrà visualizzato così:
Codice sorgente - presumibilmente VB.NET

  1. Async Function ProcessURLAsync(url As String, client As HttpClient, ct As CancellationToken) As Task(Of (String, String))
  2.         Dim maxRetries As Integer = 3  ' Numero massimo di tentativi
  3.         For attempt As Integer = 1 To maxRetries
  4.             Try
  5.                 Dim response As HttpResponseMessage = Await client.GetAsync(url, ct)
  6.                 Using content As HttpContent = response.Content
  7.                     Dim urlContents As String = Await content.ReadAsStringAsync(ct)
  8.                     Return (url, urlContents)
  9.                 End Using
  10.             Catch ex As HttpRequestException
  11.                 If attempt < maxRetries Then
  12.                     Task.Delay(1000).Wait()     ' Ritenta dopo un secondo di attesa
  13.                 Else
  14.                     Throw New Exception("Errore di I/O dopo i tentativi di ritentativo.")
  15.                 End If
  16.             Catch ioEx As IOException
  17.                 If attempt < maxRetries Then
  18.                     Task.Delay(1000).Wait()      ' Ritenta dopo un secondo di attesa
  19.                 Else
  20.                     Throw New Exception("Errore di I/O dopo i tentativi di ritentativo.")
  21.                 End If
  22.             Catch ex As Exception
  23.                 MessageBox.Show("Errore generale: " & ex.Message)
  24.             End Try
  25.         Next
  26. End Function



Specifica gli Imports che hai usato e indica se hai inserito nei riferimenti qualche .dll, altrimenti così è ambiguo.

Dacci l'URL se non è privato, i siti non rispondono tutti allo stesso modo.


Ultima modifica effettuata da Carlo il 06/12/2023 alle 21:37


in programmazione tutto è permesso
PM Quote