简体   繁体   中英

Trying to handle the file existence in vb.net vs2010

Have a piece of logic that looks like that:

Dim fInfo As New FileInfo(LocationDir & Filename)
If Not fInfo.Exists Then
    Console.WriteLine("File does not exist." & Filename)
    Throw New Exception("File does not exist." & Filename)
End If

That supposed to throw the exception, stop the execution of the whole module, and get out. Understand it is not a peaceful ending. When I am getting is that there is unhandled error. and besides the console , another box pops up: that the application failed, and needs to be closed or debugged. I guess I need to handle the graceful termination of the module. Any suggestions?

Roland asked a great question where do I catch that user exception. And I am being totally honest that after searching and reading a lot, still cannot understand where and how I can catch it. It could be very trivial for experienced users, but would love to have a piece of code to use as a base.

It is probably easier to use the IO.FileExists function and the IO.Path.Combine function to build the path.

If IO.File.Exists(IO.Path.Combine(LocationDir, Filename)) Then
   'code
End If

I at least never had a problem with it. Also like, Rowland wrote, you of course have to catch the exception you throw in your code somewhere if you forgot.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM