bangbang023 Veteran Posted April 11, 2003 Veteran Share Posted April 11, 2003 How do I handle the filenotfoundexception in vb .net? Like, if the file is not found, I want the program to output an error message to the label it was going to fill oriinally with the data from the file. Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/ Share on other sites More sharing options...
0 devgrp Posted April 12, 2003 Share Posted April 12, 2003 Try this Try File.Open("myfile.txttch ex As FileNotFoundException lblMessage.Text = "File not found" End Try Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-762638 Share on other sites More sharing options...
0 Glowstick Posted April 12, 2003 Share Posted April 12, 2003 Exceptions are your friend! Don't hesitate to use and drop them in your own selfmade classes, they're not creating nearly as much overhead as in e.g. C++ due their managed nature in .NET Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-763325 Share on other sites More sharing options...
0 bangbang023 Veteran Posted April 13, 2003 Author Veteran Share Posted April 13, 2003 thanx, I'm not really making my own classes but in my program, the program has to read alot of files and instead of crashing I would prefer to send out an error message describing the issue to help myself deal with supporting the software. Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-765344 Share on other sites More sharing options...
0 Steven Posted April 13, 2003 Share Posted April 13, 2003 bangbang023 said: thanx, I'm not really making my own classes but in my program, the program has to read alot of files and instead of crashing I would prefer to send out an error message describing the issue to help myself deal with supporting the software. if you have no classes you have no application. :blink: You also have an ACCESS DENIED BUG you need to address. Let me tell you this now, your DNote App doesn't work in a Domain Environment at all. To view this change your permissions from ADMINSTRATOR to USER and then your application will crash like a mofo. Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-765368 Share on other sites More sharing options...
0 bangbang023 Veteran Posted April 13, 2003 Author Veteran Share Posted April 13, 2003 ahh sorry I'm thinking back to C++ and the whole class thing and all. my mistake Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-765425 Share on other sites More sharing options...
0 bangbang023 Veteran Posted April 13, 2003 Author Veteran Share Posted April 13, 2003 (edited) devgrp said: Try this Try File.Open("myfile.txttch ex As FileNotFoundException lblMessage.Text = "File not found" End Try thanx I finally found my answer in MSDN thought. Your code wasn't 100% correct. Quote Try FileOpen(1, skinChoice & "\readme.txt", OpenMode.Input) lblNoteInfo.Text = LineInput(1) Do Until EOF(1) lblNoteInfo.Text += vbCrLf & LineInput(1) Loop FileClose(1) Catch e As Exception lblNoteInfo.Text = "No Skin Information Available" End Try Edited April 13, 2003 by bangbang023 Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-765575 Share on other sites More sharing options...
0 devgrp Posted April 13, 2003 Share Posted April 13, 2003 Why are you using the VB6 way of opening the file? Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-765981 Share on other sites More sharing options...
0 pagal Posted April 13, 2003 Share Posted April 13, 2003 bangbang023 said: devgrp said: Try this Try File.Open("myfile.txttch ex As FileNotFoundException lblMessage.Text = "File not found" End Try thanx I finally found my answer in MSDN thought. Your code wasn't 100% correct. Quote Try? ? ? ? ? ? FileOpen(1, skinChoice & "\readme.txt", OpenMode.Input) ? ? ? ? ? ? lblNoteInfo.Text = LineInput(1) ? ? ? ? ? ? Do Until EOF(1) ? ? ? ? ? ? ? ? lblNoteInfo.Text += vbCrLf & LineInput(1) ? ? ? ? ? ? Loop ? ? ? ? ? ? FileClose(1) ? ? ? ? Catch e As Exception ? ? ? ? ? ? lblNoteInfo.Text = "No Skin Information Available" ? ? ? ? End Try actually, his code is correct and concise... use this syntax for Try-Catch block Try ?normal code Catch exc1 As FirstException ?exception-handling code for FirstException Catch exc2 As SecondException ?exception-handling code for SecondException . . Catch ?exception-handling code for any remaining exceptions Finally ?clean-up code End Try the only problem with his code was the FileOpen syntax Try ? ? FileOpen(1, "\myfile.txt", OpenMode.Input) Catch exp As IO.FileNotFoundException ? ? lblMessage.Text = "File not found" End Try and then you can change whatever in the Try block, i.e. do whatever you want with the file. for example, you could use the Do Until EOF OR use a StreamReader and then use sr.peek way. Link to comment https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/#findComment-766036 Share on other sites More sharing options...
Question
bangbang023 Veteran
How do I handle the filenotfoundexception in vb .net? Like, if the file is not found, I want the program to output an error message to the label it was going to fill oriinally with the data from the file.
Link to comment
https://www.neowin.net/forum/topic/71964-vb-net-handling-filenotfoundexception/Share on other sites
8 answers to this question
Recommended Posts