VB.Net program to demonstrate the FormatException

Here, we are going to demonstrate the FormatException in VB.Net.
Submitted by Nidhi, on January 07, 2021

We will demonstrate FormatException, here we will print a decimal number using WriteLine() method in the invalid format.

Program/Source Code:

The source code to demonstrate the FormatException is given below. The given program is compiled and executed successfully.

'Vb.Net program to demonstrate the FormatException.

Imports System.IO

Module Module1
    Sub Main()
            Dim Num As Decimal = 145.85
            Console.WriteLine("The number is {0:Q2}.", Num)
        Catch ex As FormatException
        End Try
    End Sub
End Module


Format specifier was invalid.
Press any key to continue . . .


In the above program, we created a module Module1 that contains a Main() function.

The Main() function is the entry point for the program. Here, we created a decimal variable Num, which is initialized with the value "145.85" then we printed the variable in the invalid format then FormatException gets generated and caught in the catch block and prints the appropriate message on the console screen.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

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.