ADVERTISEMENT
ADVERTISEMENT

VB.Net program to demonstrate the finally block in exception handling

Here, we are going to demonstrate the finally block in exception handling in VB.Net.
Submitted by Nidhi, on January 05, 2021

Here, we demonstrate the finally block in exception handling. The finally is always executed in the program either an exception is generated or not.

Program/Source Code:

The source code to demonstrate the finally block in exception handling is given below. The given program is compiled and executed successfully.

'Vb.Net program to demonstrate the finally block.

Module Module1
    Class Sample
        Public Sub SayHello()
            Console.WriteLine("Hello World")
        End Sub
    End Class

    Sub Main()
        Try
            Dim S As New Sample()

            S = Nothing
            S.SayHello()
        Catch e As NullReferenceException
            Console.WriteLine(e.Message)
        Finally
            Console.WriteLine("Finally block gets executed")
        End Try
    End Sub
End Module

Output:

Object reference not set to an instance of an object.
Finally block gets executed
Press any key to continue . . .

Explanation:

In the above program, here we created a module Module1. In Module1 we created a class Sample that contains SayHello() method.

The Main() is the entry point for the program, here we created the object of Sample class and then assigned the Nothing in the object and then called the SayHello() method then it will generate null reference exception that will be caught by catch block and print an appropriate message on the console screen. After that finally block get executed.

VB.Net Exception Handling Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.