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 360 views
2 answers 414 views
414 views asked May 4, 2015 by avibootz
6 answers 677 views
677 views asked May 4, 2015 by avibootz
1 answer 284 views
284 views asked Mar 7, 2017 by avibootz
1 answer 144 views
144 views asked Oct 8, 2022 by avibootz
1 answer 169 views
169 views asked Jul 11, 2022 by avibootz
...