VB.Net program to demonstrate the DirectoryNotFoundException

Here, we are going to demonstrate the DirectoryNotFoundException in VB.Net.
Submitted by Nidhi, on January 07, 2021 [Last updated : March 08, 2023]

DirectoryNotFoundException in VB.Net

Here, we will demonstrate the DirectoryNotFoundException, if the specified directory is not found that are accessing in the program, then the DirectoryNotFoundException get occurred.

Program/Source Code:

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

VB.Net code to demonstrate the example of DirectoryNotFoundException

'Vb.Net program to demonstrate 
'the DirectoryNotFoundException.

Imports System.IO

Module Module1
    Sub Main()
        Try
            Dim direct As String = "C:\Sample"
            Directory.SetCurrentDirectory(direct)
        Catch ex As DirectoryNotFoundException
            Console.WriteLine("Directory not found exception occurred")
        End Try
    End Sub
End Module

Output

Directory not found exception occurred
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 used the SetCurrentDirectory() method of Directory class to set the current directory. If the specified directory does not exist in the system then DirectoryNotFoundException gets generated and prints the appropriate message on the console screen.

VB.Net Exception Handling Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.