jcolby at colbyconsulting.com
jcolby at colbyconsulting.com
Mon Jul 14 16:18:45 CDT 2003
Bob, But this brings up my question, suppose that on the first attempt to add an object to the collection, it is found? The catch runs, but is processing returned to the next line below the error? That would be (in this scenario) the preferred action so a "resume next" would be required. John W. Colby www.colbyconsulting.com -----Original Message----- From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com]On Behalf Of Bob Bedell Sent: Monday, July 14, 2003 4:40 PM To: accessd at databaseadvisors.com Subject: Re: [AccessD] OT: VB.Net - general questions Hi John, Put together a little console app below to demonstrate the little I know about multiple catch clauses. Instead of using an error number, simply use the name of an Exception class exception type. I use NullReferenceException (collection doesn't exist scenario) and ArgumentException (duplcate object key value scenario) below. The Finally keyword handles clean up for all three of the catch statements. Not sure how to implement it in a "Exit_Here:" style label. Unclear about a "Resume Next" replacement also. Interested to know what you, or others, might uncover. Bob Module Module1 ' Declare module-level collection. Private mCollection As New Collection() Sub Main() Dim oObject1 As New Object() Dim oObject2 As New Object() Dim oObject3 As New Object() ' Un-comment next line to test for NullReferenceException() 'mCollection = Nothing Try ' Assign objects to module-level collection. mCollection.Add(oObject1, "One") mCollection.Add(oObject2, "Two") mCollection.Add(oObject3, "Three") 'Uncomment next line to test for ArgumentException 'mCollection.Add(oObject1, "Three") ' duplicate key value 'Multiple catch clauses ending with "catch all" to ensure all ' exceptions are handled. Catch exNRF As NullReferenceException Console.WriteLine("Collection does not exist") Catch exARG As ArgumentException Console.WriteLine("Duplicate key value found") Catch ex As Exception Console.WriteLine("Some other exception found: " & ex.ToString()) Finally '...Runs in all of the above cases. Can clean up objects here. End Try Console.WriteLine("") Console.WriteLine(">>> Press Enter to terminate the program <<<") Console.ReadLine() End Sub End Module >From: <jcolby at colbyconsulting.com> >Reply-To: Access Developers discussion and problem >solving<accessd at databaseadvisors.com> >To: "VBA" <dba-vb at databaseadvisors.com>, "AccessD" ><AccessD at databaseadvisors.com> >Subject: [AccessD] OT: VB.Net - general questions >Date: Mon, 14 Jul 2003 14:16:56 -0400 > >I am porting my SysVars class to VB.Net just as an exercise. Of course I >use the error handler insertion wizard for Access, but VB.Net has the new >Try/catch error handling (which I very much like BTW). But it does mean >that no port is trivial since I have to remove all the OnError / resume >kind >of stuff in every function. > >In my old code I have a case statement where I accumulated the various >errors that could occur, and once handled a resume next would take me back >into the code. that obviously has to change but I'm a little confused as >to >what it will change to. For example I have code that attempts to add an >object to a collection: > > mcolObjNames.Add(strObjName, strObjName) > >There could be two problems here, the first is that no collection has been >instantiated yet, the second is that the object is already in the >collection. Thus a select case would be nice, which was how the old error >handler in Access worked. > >Select case err > case XXX > case YYY > Case else >end select > >Now we have: > >Try > > mcolObjNames.Add(strObjName, strObjName) > >Catch e as XXXX (FIRST PROBLEM - WHAT IS xxxx?) > Handle error here >Finally > >End Try > >It certainly looks like e could be used in a select case statement. >However >to do so I need the equivalent of the "resume next". > > >AND FINALLY... > >In access I also used a label for the exit such that all exiting code could >go through the exit point for cleanup of pointers etc. The books I have >don't ever show such a construct. > > >John W. Colby >www.colbyconsulting.com