Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the FormatException
By Nidhi Last Updated : November 11, 2024
FormatException in VB.Net
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 code to demonstrate the example of FormatException
'Vb.Net program to demonstrate the FormatException.
Imports System.IO
Module Module1
Sub Main()
Try
Dim Num As Decimal = 145.85
Console.WriteLine("The number is {0:Q2}.", Num)
Catch ex As FormatException
Console.WriteLine(ex.Message)
End Try
End Sub
End Module
Output
Format specifier was invalid.
Press any key to continue . . .
Explanation
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.
VB.Net Exception Handling Programs »