My question concerns using, 'Throw New Exception' in the Catch Block.
I have seen MessageBox("message") used in some Catch blocks, and alternatively Throw New Exception("message").
Is there a difference? Why would I use Throw New Exception and not use a message box instead.
MessageBox
shows a message box. If someone writes code in that way, it implies that an exception was caught, and the users needs to be informed.
Throwing in catch
block usually is used to rename exceptions. For example, you're reading a file. Reading a file can raise a range of exceptions from "file is already open" to "device is not ready". But your code wants to report to the outer code that either it "couldn't open file" or "file has a wrong format". Outer code doesn't care too much on the reason why the file couldn't be opened.
So, exceptions while opening the file are caught, and then one "couldn't open file" exception is thrown upwards, so that outer code doesn't need to handle all of those exceptions.
Sub OuterCode()
Try
Dim s As String
s = ReadFile()
ParseFile(s)
Catch e As CouldntReadFileException
' ...
Catch e As ParsingException
' ...
End Try
End Sub
Function ReadFile() As String
Try
' Open file
Catch e
Throw New CouldntReadFileException()
End Try
End Function
Sub ParseFile(s As String)
...
End Sub