Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am using streamreader to read from a file upon startup, but if this file doesnt exist, or is empty the program crashes at startup.

 

This file is a log file, and is created by the program at a later point.

 

I could solve the problem by putting the declaration for the streamreader in an if statement which checks if file exists, but i am old fashioned, and this just seems wrong.

 

So what i am basically asking it is whether i can stop it from crashing if the file doesnt exist. Heres is the declaration line

 

Dim sr As New StreamReader(LogPath & ".hist")

 

Thanks.

  • Administrators
Posted

Dim sr as streamreader
try
   sr = New StreamReader(LogPath & ".hist")
catch 
   'handle error
end try

would handle the error but why not just carry on checking first, not sure why making sure something can be done before doing it is 'old fashioned'.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thank you!

 

I have always just put ALL declarations at the top of the coding, as has all my colleagues, i can be quite flexable to make life easier, but it would make it alot harder for my colleagues to understand, if i was ill. Considering most of them last used VB back in the days of version 3. :)

Posted

Yeah thats what i done after i saw your first post.

 

Thanks for you help again. I am glad they made you a 'Forum Leader' as you always seem to be the person who solves my idiotic problems, you might as well get some recognisition for it :)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...