Private Sub Test Dim a As Integer = 10 Dim b As Integer = 0 Dim c As Double c = a / b 'ici va se déclencher ton erreur division par zéro impossible End Sub
Private Sub Test Dim a As Integer = 10 Dim b As Integer = 0 Dim c As Double Try c = a / b Catch ex As Exception MessageBox.Show("b ne doit pas valoir zéro") End Try End Sub
Private Sub Test Dim a As Integer = 10 Dim b As Integer = 0 Dim c As Double If b = 0 Then MessageBox.Show("b ne doit pas valoir zéro") Else c = a / b End If End Sub
'dans une nouvelle class par défaut Public Class Class1 Public Function Division(ByVal a As Integer, ByVal b As Integer) As Double Return a / b End Function End Class 'dans la form Private Sub Test Dim a As Integer = 10 Dim b As Integer = 0 Dim c As Double Dim m As New Class1 c = m.Div(a, b) End Sub
Public Function Div(ByVal a As Integer, ByVal b As Integer) As Double If b = 0 Then Return 0.0# 'ou Nothing éventuellement Else Return a / b End If End Function
Public Class Class1 Public Enum DivZeroConstants ThrowException DoNothing End Enum Public Function Div(ByVal a As Integer, ByVal b As Integer, ByVal actionOnZero As DivZeroConstants) As Double If b = 0 Then If actionOnZero = DivZeroConstants.DoNothing Then Return Nothing Else 'on relève à la demande de l'appelant Throw New DivideByZeroException() End If Else Return a / b End If End Function End Class
Private Sub Test Dim a As Integer = 10 Dim b As Integer = 0 Dim c As Double Dim d As Double Dim m As New Class1 'on pourrait tester zéro mais pas besoin parce que par exemple, 'on n'exécute pas le reste du code si c=nothing c = m.Div(a, b, Class1.DivZeroConstants.DoNothing) If c = Nothing Then Console.WriteLine("Test.InstanceCalcul.Div = b vaut zéro, fin d'exécution de la procédure") 'Exit Sub End If 'on veut tester ZERO par l'exception Try d = m.Div(a, b, Class1.DivZeroConstants.ThrowException) Catch ex As Exception 'on pourrait mettre cette ligne dans un log Console.WriteLine("ERREUR Test.InstanceCalcul.Div = {0}", ex.Message) 'ex.message vaut "ERREUR Test.InstanceCalcul.Div = Tentative de division par zéro." End Try End Sub
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionsoit tu relèves une erreur, ce qui est le cas ici (UICancelOption.ThrowException), que tu interceptes, soit tu ne fais rien (UICancelOption.DoNothing)
My.Computer.FileSystem.DeleteFile(ShosenFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException)
' Supprimer la note Private Sub SupprimeNote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SupprimeNote.Click ' Chemin du fichier Dim ShosenFile As String = Me.Tag.ToString ' Sppression => corbeille Try My.Computer.FileSystem.DeleteFile(ShosenFile, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException) ' Catch 'ex As OperationCanceledException Catch ex As Exception MessageBox.Show(ex.Message) Exit Sub End Try DirectCast(GetControlByName(Form1, "N" & SauveEtQuitter.Tag.ToString), Label).Text = Empty 'efface logo Me.Close() End Sub
' Supprimer la note Private Sub SupprimeNote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SupprimeNote.Click Try My.Computer.FileSystem.DeleteFile(Me.Tag.ToString, Microsoft.VisualBasic.FileIO.UIOption.AllDialogs, Microsoft.VisualBasic.FileIO.RecycleOption.SendToRecycleBin, Microsoft.VisualBasic.FileIO.UICancelOption.ThrowException) DirectCast(GetControlByName(Form1, "N" & SauveEtQuitter.Tag.ToString), Label).Text = String.Empty 'efface logo Me.Close() Catch ex As Exception 'OperationCanceledException MessageBox.Show("Suppression annulée") End Try End Sub
Throw new exception("blablabla")
"Il est même parfois nécessaire de les provoquer volontairement"