Autore Topic: Quando devo aspettarmi che "Catch" non rilevi errori ?  (Letto 431 volte)

Offline vuott

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 11.373
  • Ne mors quidem nos iunget
    • Mostra profilo
« Chiunque, non ricorrendo lo stato di necessità, nel proprio progetto Gambas fa uso delle istruzioni Shell o Exec, è punito con la sanzione pecuniaria da euro 20,00 a euro 60,00. »

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.161
  • Tonno verde
    • Mostra profilo
Re:Quando devo aspettarmi che "Catch" non rilevi errori ?
« Risposta #1 il: 30 Ottobre 2019, 13:21:19 »
Ma scusa se lui usa Settings in modo non convenzionale è logico che non possa intercettare l'errore  :-\

O forse non ho capito cosa cerca...

Codice: [Seleziona]
Public Sub Main()
 
    Dim sKey As String = "qq" '"x" "qq"
 
    If OpenFromSettings1(sKey) Then
        Print "OpenFromSettings1 OK"
    Else
        Print "OpenFromSettings1 not OK"
    Endif
 
    Print "------------------"
 
    If OpenFromSettings2(sKey) Then
        Print "OpenFromSettings2 OK"
    Else
        Print "OpenFromSettings2 not OK"
    Endif
 
    Print "------------------"
 
    If OpenFromSettings3(sKey) Then
        Print "OpenFromSettings3 OK"
    Else
        Print "OpenFromSettings3 not OK"
    Endif
Catch
    Debug Error.Text
    Print "anything"
 
End
 
Public Function OpenFromSettings1(key As String) As Boolean
 
    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer
 
    If IsNull(key) Then
        Error.Raise("OpenFromSettings1: Key is null")
    Endif
 
    sValue = Settings[key & "/SomeString", key]
    Print sValue
 
    bValue = Settings[key & "/SomeBoolean", True]
    Print bValue
 
    iValue = Settings[key & "/SomeInteger", 5]
    Print iValue
 
    returnValue = True
Finally
    Return returnValue
Catch
    Debug Error.Text
    Print "anything"
 
End
 
Public Function OpenFromSettings2(key As String) As Boolean
 
    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer
 
    If IsNull(key) Then
        Error.Raise("OpenFromSettings2: Key is null")
    Endif
 
    sValue = Settings[key & "/SomeString", key]
    Print sValue
 
    bValue = Settings[key & "/SomeBoolean", True]
    Print bValue
 
    iValue = Settings[key & "/SomeInteger", 33]
    Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
 
    ' Try on statement followed by If Error will see error
 
    returnValue = True
Finally
    Return returnValue
Catch
    Debug Error.Text
    Print "anything"
 
End
 
Public Function OpenFromSettings3(key As String) As Boolean
 
    Dim returnValue As Boolean = False
    Dim sValue As String
    Dim bValue As Boolean
    Dim iValue As Integer
 
    If IsNull(key) Then
        Error.Raise("OpenFromSettings3: Key is null")
    Endif
 
    Try sValue = Settings[key & "/SomeString", key]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print sValue
 
    Try bValue = Settings[key & "/SomeBoolean", False]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print bValue
 
    Try iValue = Settings[key & "/SomeInteger", 1]
    If Error Then
        Debug Error.Text
        Return False
    Endif
    Print iValue 'IIf(IsNull(Settings[key & "/SomeInteger"]), 0, Settings[key & "/SomeInteger"])
 
    returnValue = True
Finally
    Return returnValue
    ' Catch
    '     Debug Error.Text
    ' Print "anything"
 
End
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro

Offline Gianluigi

  • Moderatore globale
  • Senatore Gambero
  • *****
  • Post: 4.161
  • Tonno verde
    • Mostra profilo
Re:Quando devo aspettarmi che "Catch" non rilevi errori ?
« Risposta #2 il: 30 Ottobre 2019, 14:05:43 »
Giusto per essere più chiari se io scrivo questo codice:

Codice: [Seleziona]
Public Sub Main()

  Dim s As String = Settings["Pippo/Pappa"]
 
  Print s

End
non si solleva un errore perché è come se io scrivessi:

Codice: [Seleziona]
Dim s As String = Null

Vale a dire che ogni tipo di dato che supporta Null non può sollevare errore se messo a Null

Ma se io scrivo:

Codice: [Seleziona]
Public Sub Main()

  'Dim s As String = Null
  Dim i As Integer = Settings["Pippo/Pappa"]
 
  Print i
Catch
  Debug Error.Text

End
Catch giustamente intercetta l'errore.
nuoto in attesa del bacio di una principessa che mi trasformi in un gambero azzurro