Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use the Try/Catch block to catch exceptions in VB.NET

2 Answers

0 votes
Imports System.IO

Module Module1

    Sub Main()

        Try

            Dim sr As StreamReader = File.OpenText("c:\\data.txt")
            Console.WriteLine("The first text:  {0}", sr.ReadLine())
            sr.Close()

        Catch e As Exception

            Console.WriteLine("{0} - {1}", e.GetType(), e.Message)

        End Try

    End Sub

End Module

' run:
' 
' System.IO.FileNotFoundException - Could not find file 'c:\data.txt'.

 



answered Apr 9, 2016 by avibootz
0 votes
Imports System.IO

Module Module1

    Sub Main()

        Try

            Dim sr As StreamReader = File.OpenText("c:\\data.txt")
            Console.WriteLine("The first text:  {0}", sr.ReadLine())
            sr.Close()

        Catch e As Exception

            Console.WriteLine(e)

        End Try

    End Sub

End Module

' run:
' 
' System.IO.FileNotFoundException: Could not find file 'c:\data.txt'.
' File name: 'c:\data.txt'
'    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
'    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, I
' nt32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions o
' ptions, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolea
' n useLongPath, Boolean checkHost)
'    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access,
' FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean
' bFromProxy, Boolean useLongPath, Boolean checkHost)
'    at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detec
' tEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
'    at System.IO.StreamReader..ctor(String path)
'    at System.IO.File.OpenText(String path)
'    at ConsoleApplication_Visual_Basic.Module1.Main() In D:\directx\ConsoleApplic
' ation_Visual_Basic\ ConsoleApplication_Visual_Basic \ Module1.vb: line 9

 



answered Apr 9, 2016 by avibootz

Related questions

2 answers 401 views
2 answers 269 views
1 answer 190 views
1 answer 168 views
168 views asked Dec 17, 2016 by avibootz
1 answer 195 views
195 views asked Jan 22, 2021 by avibootz
2 answers 350 views
350 views asked May 4, 2015 by avibootz
6 answers 635 views
635 views asked May 4, 2015 by avibootz
...