How to exit a method in VB.NET

2 Answers

0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Try
            Console.WriteLine(f(3))
            Console.WriteLine(f(0))
        Catch ex As ArgumentException
            Console.WriteLine("Err: " & ex.Message)
        End Try
    End Sub

    Public Shared Function f(ByVal n As Integer) As Integer
        If n = 0 Then
            Throw New ArgumentException("The value 0 is not accepted.")
        End If

        Return n * 100
    End Function
End Class



' run:
'
' 300
' Err: The value 0 is not accepted.
'

 



answered Mar 20, 2025 by avibootz
0 votes
Imports System

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Console.WriteLine([function](3))
        Console.WriteLine([function](0))
    End Sub

    Public Shared Function [function](ByVal n As Integer) As Integer
        If n = 0 Then
            Console.WriteLine("Err: The value 0 is not accepted. Returning -1.")
            Return -1
        End If

        Return n * 100
    End Function
End Class



' run:
'
' 300
' Err: The value 0 is not accepted. Returning -1.
' -1
'

 



answered Mar 20, 2025 by avibootz

Related questions

1 answer 164 views
164 views asked Jun 23, 2025 by avibootz
1 answer 277 views
1 answer 297 views
297 views asked Jul 20, 2019 by avibootz
2 answers 187 views
187 views asked Mar 20, 2025 by avibootz
2 answers 254 views
254 views asked Nov 9, 2023 by avibootz
...