How to use nested try catch in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text

Public Module Module1
	Public Sub Main()
		Try
			Try
				Dim sb As StringBuilder

				sb.Append("vb.net")
			Catch ex1 As Exception
				Throw New ApplicationException("Catch ex1: ", ex1)
			End Try
		Catch ex2 As Exception
			Console.WriteLine("Catch ex2: " & ex2.Message)
			Console.WriteLine("ex2.StackTrace: " & ex2.StackTrace)

			If ex2.InnerException IsNot Nothing Then
				Console.WriteLine("ex2.InnerException: " & ex2.InnerException.Message)
				Console.WriteLine("ex2.StackTrace: " & ex2.StackTrace)
			End If
    	End Try
	End Sub
End Module



' run:
'
' Catch ex2: Catch ex1: 
' ex2.StackTrace:    at Module1.Main()
' ex2.InnerException: Object reference not set to an instance of an object.
' ex2.StackTrace:    at Module1.Main()
'

 



answered Jan 22, 2021 by avibootz

Related questions

2 answers 368 views
2 answers 420 views
420 views asked May 4, 2015 by avibootz
6 answers 682 views
682 views asked May 4, 2015 by avibootz
1 answer 286 views
286 views asked Mar 7, 2017 by avibootz
1 answer 145 views
145 views asked Oct 8, 2022 by avibootz
1 answer 174 views
174 views asked Jul 11, 2022 by avibootz
...