Table of Contents
hide
Example : Exception Handling Examples in VB .Net to show the concept of System Exception Handling.
Module Module1
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch ex As DivideByZeroException
Console.WriteLine("The arised Exception is = : {0}", ex)
Finally
Console.WriteLine("The output is =: {0}", result)
End Try
End Sub
Sub Main()
division(40, 0)
'division(40, 5)
Console.ReadKey()
End Sub
End Module
----------- OR -----------
Module Module1
Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
Dim result As Integer
Try
result = num1 \ num2
Catch ex As Exception
Console.WriteLine("The arised Exception is = : {0}", ex)
Finally
Console.WriteLine("The output is =: {0}", result)
End Try
End Sub
Sub Main()
division(40, 0)
'division(40, 5)
Console.ReadKey()
End Sub
End Module
Output:
The arised Exception is = : System.DivideByZeroException: Attempted to divide by zero.
at ConsoleApplication2.Module1.division(Int32 num1, Int32 num2) in C:\Users\RAJ\Desktop\ConsoleApplication2\ConsoleApplication2\Module1.vb:line 5
The output is =: 0
Example : Exception Handling Examples in VB .Net to show the User Defined Exception Handling concept.
Imports System
Module Module1
Sub Main()
Dim str1 As String = "Welcome"
Try
If str1.Length > 0 Then 'Not throws an exception when condition is True
Console.WriteLine(" The String is {0}", str1)
End If
Catch ex As Exception ' it will cacthe an exception
Console.WriteLine("The exception message is {0}", ex.Message)
End Try
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Output:
The String is Welcome
Press any key to exit...
------------------ OR ------------------
Imports System
Module Module1
Sub Main()
Dim str1 As String = Nothing
Try
If str1.Length > 0 Then 'throws an exception when condition is False
Console.WriteLine(" The String is {0}", str1)
End If
Catch ex As Exception ' it will cacthe an exception
Console.WriteLine("The exception message is {0}", ex.Message)
End Try
Console.WriteLine(" Press any key to exit...")
Console.ReadKey()
End Sub
End Module
Output:
The exception message is Object reference not set to an instance of an object.
Press any key to exit...
------------------ OR ------------------
Module Module1
Sub Main()
Try
Throw New ApplicationException("Message for a User defined exception")
Catch ex As Exception
Console.WriteLine("The user defined exception is : " & ex.Message)
Finally
Console.WriteLine("The Finally Block is executed")
End Try
Console.ReadKey()
End Sub
End Module
Output:
The user defined exception is : Message for a User defined exception
The Finally Block is executed
Example : Exception Handling Examples in VB .Net to show the concept of Multiple Catch Statements.
Imports System
Module Module1
Sub Main()
Dim value1 As Integer = 0
Dim value2 As Integer = 0
Dim result As Integer = 0
Try
Console.Write("Enter First Value: ")
value1 = Integer.Parse(Console.ReadLine())
Console.Write("Enter Second Value: ")
value2 = Integer.Parse(Console.ReadLine())
result = value1 \ value2
Console.WriteLine("result : " + result)
Catch e As DivideByZeroException
Console.WriteLine(e.Message)
Catch e As FormatException
Console.WriteLine("Format exception")
Catch e As Exception
Console.WriteLine("Other exception")
End Try
End Sub
End Module
Output:
Enter First Value: 10
Enter Second Value: 0
Attempted to divide by zero.
Enter First Value: 12
Enter Second Value: 3.2
Format exception
Enter First Value: 10
Enter Second Value: 2
Other exception
0 Comments