BP Forums
Exceptions help - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: Exceptions help (/showthread.php?tid=799)



Exceptions help - brco900033 - 08-04-2013

Hello everyone

I have two questions that are bothering me now for a long time but can't find any answers to it. These questions are about Exceptions in VB.NET.

- I know what the Try...Catch block does, it catches any errors, but how far do you have to go? For example you want to open a file but it is
possible that the file doesn't exist then you use the Try...Catch, I know that. Is it also needed for example changing the text of a textbox or editing items in a treeview...

- The second question is about the Finally block. Let's say we've downloaded a file but there occurres an error during the downloading process, the Catch block catches the error... and then I want to remove the downloaded file if it exists. Then there is a possibility that the file doesn't exist so if I write
Code:
My.Computer.DeleteFile(pathFile)
in the Finally block it throws a second exception right? or does VB.NET take care of that automatically?


Thanks, Brecht


Re: Exceptions help - brandonio21 - 08-04-2013

To answer your first question, it is generally a good idea to make sure that your program is free of obvious errors instead of handling them with Try/Catch blocks. For example, you can use a Try/Catch block to catch any errors that may occur during the reading of a file if the file doesn't exist, but it would be much better to do something like this:
[code2=vb.net]If (My.Computer.Filesystem.FileExists(path)) Then
My.Computer.Filesystem.DeleteFile(path)
Else
MsgBox("The file does not exist!")
End If[/code2]
In this way, you are not forcing the program to break its current flow due to exceptions and you are also catching the error and letting the user know what the problem is. This can be done for anything, for example, an "Object Reference" error when selecting items from a ListBox
[code2=vbnet]Try
MsgBox(ListBox1.SelectedItem.ToString)
Catch ex As Exception

End Try[/code2]
The above code definitely catches the error, but it doesn't really do anything about it. This is considered bad practice. We want to ensure that everything is okay before calling ListBox1.SelectedItem.ToString in order to prevent any errors from occurring. So a better solution would be:
[code2=vbnet]If Not (ListBox1.SelectedItem Is Nothing) Then
MsgBox(ListBox1.SelectedItem.ToString)
End If[/code2]
But it really all depends on a person's programming style.


To answer your second question, the finally block does not disregard any actual errors that would occur, so it would throw a second exception in your specific case. One way to prevent this is by using the above methods for checking/preventing errors. So something like this would solve your problem
[code2=vb.net]Try
My.Computer.Network.DownloadFile(localPath, remotePath)
Catch ex As Exception
'Some error occurred during the download!
Finally
If (My.Computer.Filesystem.FileExists(localPath)) Then
My.Computer.Filesystem.DeleteFile(localPath)
End If
End Try[/code2]

In this way, you are catching any error that may occur during the download and also deleting the file if it exists.

I hope this helps!


Re: Exceptions help - brco900033 - 08-05-2013

That is one clean reply <!-- sBig Grin --><img src="{SMILIES_PATH}/icon_e_biggrin.gif" alt="Big Grin" title="Very Happy" /><!-- sBig Grin -->

Never really thought of detecting the errors without using the Try function. That is actually really handy, now I can make the code much cleaner... also I can try to make better solutions so the process won't be interrupted by something stupid that is easy to fix.

Thanks man, I understand fully now!


Re: Exceptions help - brandonio21 - 08-05-2013

I am very glad that I could help you! As you may have already noticed, exceptions in any programming language are extremely annoying. Especially since they stop the program from doing anything it is currently doing. Because of this, it is in better taste to prevent the exception from being thrown instead of catching it. I have found a lot of success with this method.

Have a good one!