How to use Try Catch Finally in VB.NET

2 Answers

0 votes
Imports System
Imports System.IO

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim x As Integer = 3
        Dim y As Integer = 1
        Dim n As Integer
 
        Try
            n = x \ y
            Console.WriteLine("n = {0}", n)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine("Stack Trace: " & vbCrLf & ex.StackTrace)
        Finally
            Console.WriteLine("Finally")
        End Try
    End Sub
End Class

 
 
 
' run:
'
' n = 3
' Finally4
'


answered May 4, 2015 by avibootz
edited May 14, 2024 by avibootz
0 votes
Imports System
Imports System.IO

Public Class Program
    Public Shared Sub Main(ByVal args As String())
        Dim x As Integer = 3
        Dim y As Integer = 0
        Dim n As Integer
 
        Try
            n = x \ y
            Console.WriteLine("n = {0}", n)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            Console.WriteLine("Stack Trace: " & vbCrLf & ex.StackTrace)
        Finally
            Console.WriteLine("Finally")
        End Try
    End Sub
End Class

 
 
 
' run:
'
' Attempted to divide by zero.
' Stack Trace: 
'   at Program.Main (System.String[] args) [0x00011] in <d181028a9d974f559230f36deae1feca>:0 
' Finally
'
'


answered May 4, 2015 by avibootz
edited May 14, 2024 by avibootz

Related questions

1 answer 169 views
169 views asked Jul 11, 2022 by avibootz
1 answer 231 views
231 views asked Jun 4, 2021 by avibootz
1 answer 234 views
234 views asked Nov 23, 2020 by avibootz
1 answer 406 views
2 answers 295 views
2 answers 295 views
1 answer 167 views
...