Table of Contents                    
                hide            
            
Example : An Exception Handling program in VB .Net to show the concept of System Generalised Exception Handling.
Module Module1
    Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
        Dim result As Integer
        Try
            result = num1 \ num2
            Console.WriteLine("The output is =: {0}", result)
        Catch ex As Exception
            'Console.WriteLine("The arised Exception is = : {0}", ex)
            Console.WriteLine(ex.Message)
        Finally
            Console.WriteLine("This is a general exceptional handling program")
        End Try
    End Sub
    Sub Main()
        division(40, 0)
        'division(40, 5)
        Console.ReadKey()
    End Sub
End Module
Output:
Attempted to divide by zero.
This is a general exceptional handling program
Example : An Exception Handling program in VB .Net to show the concept of System Specific 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)
            Console.WriteLine(ex.Message)
        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:
Attempted to divide by zero.
The output is =: 0
-----------  OR  -----------
Module Module1
    Sub division(ByVal num1 As Integer, ByVal num2 As Integer)
        Dim result As Integer
        Try
            result = num1 \ num2
            Console.WriteLine("The output is = : {0}", result)
        Catch ex As DivideByZeroException
            'Console.WriteLine("The arised Exception is = : {0}", ex)
            Console.WriteLine(ex.Message)
        Finally
            Console.WriteLine("This is Exception Handling Program")
        End Try
    End Sub
    Sub Main()
        division(40, 0)
        'division(40, 5)
        Console.ReadKey()
    End Sub
End Module
Output:
Attempted to divide by zero.
This is Exception Handling Program
Example : An Exception Handling Program 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("The Result is :{0} ", result)
        Catch ex As DivideByZeroException
            Console.WriteLine()
            Console.WriteLine("Exception Generated : {0}", ex.Message)
        Catch ex As FormatException
            'Handles invalid input format (non-integer input)            
            Console.WriteLine()
            Console.WriteLine("Format Exception Generated : {0}", ex.Message)
        Catch ex As OverflowException
            ' Handles integer overflow
            Console.WriteLine()
            'Console.WriteLine("Error: The number is too large or too small.")
            Console.WriteLine("Overflow Exception Generated : {0}", ex.Message)
        Catch ex As Exception
            'Handles all other/generalised exceptions            
            Console.WriteLine("General Exception Generated : {0}", ex.Message)
        Finally
            'Code in the Finally block will always execute
            Console.WriteLine()
            Console.WriteLine("Program Execution Completed")
        End Try
    End Sub
End Module
Output:
Enter First Value: 124
Enter Second Value: 255555555555555
Overflow Exception Generated : Value was either too large or too small for an Int32.
Program Execution Completed
----------------------------------
Enter First Value: 40
Enter Second Value: 0
Exception Generated : Attempted to divide by zero.
Program Execution Completed
-----------------------------------
Enter First Value: 400
Enter Second Value: A
Format Exception Generated : Input string was not in a correct format.
Program Execution Completed
Example : An Exception Handling Program in VB .Net to show the User Defined Exception Handling concept.
Imports System
' Define a user-defined exception class
Class AgeException
    Inherits Exception
    Sub New(message As String)
        MyBase.New(message)
    End Sub
End Class
Module Module1
    Sub Main()
        Try
            Console.WriteLine("Enter your age:")
            Dim age As Integer = Convert.ToInt32(Console.ReadLine())
            ' Throw custom exception if age is invalid
            If age < 0 OrElse age > 120 Then
                Throw New AgeException("Cusotm Error Arise : Age must be between 0 and 120.")
            End If
            Console.WriteLine("Your age is valid.")
        Catch ex As AgeException
            ' Handle user-defined exception
            Console.WriteLine(ex.Message)
        Finally
            ' Code that always executes
            Console.WriteLine("Program execution complete.")
        End Try
    End Sub
End Module
Output:
Enter your age:
250
Cusotm Error Generated : Age must be between 0 and 120.
Program execution complete.
 ![]()
0 Comments