Answered by:
Catch as exception

Question
-
Hello again,
I'm new to using the catch as exception
I need to check and see if I can access a file(the file is in use) So When I try to access it the application crashes. When I can access the file without the application crashing I would like a msgbox to pop up.
Thanks in advance
Monday, May 17, 2010 1:37 PM
Answers
-
Catch ex As Exception MsgBox(ex.ToString(), MsgBoxStyle.Critical) End Try
Do you mean like this?
Samuel - Student - Newbie :]- Proposed as answer by Cor Ligthert Monday, May 17, 2010 5:01 PM
- Marked as answer by Liliane Teng Monday, May 24, 2010 2:57 AM
Monday, May 17, 2010 1:40 PM -
Hello again,
The way a TryCatch works is that you put any code that my create an error, and any code that depends on that code, in the try section. In the catch section you put code to handle any errors that are thrown from the try section. So in your case you would first put all the file access code in the try section and then in the catch section you would put the code to handle if you cannot access it...something like this:
Dim length As Integer = 0 Try length = IO.File.ReadAllBytes("SomeFile").Length Catch ex As Exception ' Some code that will run if you cannot access the file, maybe... MessageBox.Show("Unable to Access File") End Try
Bill Gates look out!- Proposed as answer by Cor Ligthert Monday, May 17, 2010 5:01 PM
- Marked as answer by Liliane Teng Monday, May 24, 2010 2:57 AM
Monday, May 17, 2010 2:42 PM
All replies
-
Catch ex As Exception MsgBox(ex.ToString(), MsgBoxStyle.Critical) End Try
Do you mean like this?
Samuel - Student - Newbie :]- Proposed as answer by Cor Ligthert Monday, May 17, 2010 5:01 PM
- Marked as answer by Liliane Teng Monday, May 24, 2010 2:57 AM
Monday, May 17, 2010 1:40 PM -
Hello again,
The way a TryCatch works is that you put any code that my create an error, and any code that depends on that code, in the try section. In the catch section you put code to handle any errors that are thrown from the try section. So in your case you would first put all the file access code in the try section and then in the catch section you would put the code to handle if you cannot access it...something like this:
Dim length As Integer = 0 Try length = IO.File.ReadAllBytes("SomeFile").Length Catch ex As Exception ' Some code that will run if you cannot access the file, maybe... MessageBox.Show("Unable to Access File") End Try
Bill Gates look out!- Proposed as answer by Cor Ligthert Monday, May 17, 2010 5:01 PM
- Marked as answer by Liliane Teng Monday, May 24, 2010 2:57 AM
Monday, May 17, 2010 2:42 PM -
This code will prompt you when the file becomes available for you to open it
Public Class Form1 Dim WithEvents Tm As New Timer Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Tm.Interval = 1000 Tm.Start() End Sub Private Sub Tm_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tm.Tick Try Using file As New IO.StreamReader("c:\tst\data.xml") Tm.Stop() MsgBox("File is available for the application") End Using Catch ex As Exception End Try End Sub End Class
Monday, May 17, 2010 3:42 PM