Yes the second approach is better. Event if it is NOT resource friendly (but it is resource friendly too).
Because you are testing the Exception Message, what if the exception message is different but exception is still IO exception. For example, IO exception occurred but the exception message is "IO Exception" while you are testing "IOException". Hence the second method is the surety that no matter what the exception message is you are actually handling them correctly. On Thu, Nov 11, 2010 at 9:50 PM, OliE <[email protected]> wrote: > Hi all, > > my co-worker and I had an argument about try catchs, so I thought > about asking everyone here since I can't find an answer over my > internets :P > > I found something I find weird in one of his try catch in his code. > Instead of having multiple catch of different error types, he made > this : > > Catch ex As Exception > If ex.Message = "IOException" Then > MsgBox("Communication Error") > Else > MsgBox(ex.Message) 'ex.Message > End If > > I told him no to do so and use multiple catchs like this : > > Catch exIO As IO.IOException > MsgBox("Communication Error") > > Catch ex As Exception > MsgBox(ex.Message) > > But he is sure his method is faster / resource friendly than mine, > while I think comparing strings takes longer than catching directly > the exception. > > So, who's right ? > > Thanks for future anwsers and please excuse my english, my main > language is french :)
