[AccessD] OT: VB.Net - general questions

Bob Bedell bobbedell15 at msn.com
Mon Jul 14 22:44:34 CDT 2003


Forgot to mention - the reason the line:

   Throw Err.GetException

thows the exception back to the calling procedure (where it is handled) is 
because
there are no Catch clauses (no Try block) in Sub LoadCollection to catch the 
thrown
exception. In this case the exception just bubbles up the call stack until 
it is caught by
a matching Catch clause (in this case, Catch exNRF As 
NullReferenceException).

Best

Bob

>From: "Bob Bedell" <bobbedell15 at msn.com>
>Reply-To: Access Developers discussion and problem 
>solving<accessd at databaseadvisors.com>
>To: accessd at databaseadvisors.com
>Subject: RE: [AccessD] OT: VB.Net - general questions
>Date: Tue, 15 Jul 2003 03:23:03 +0000
>
>Hi John,
>
>>That would be (in this scenario)
>>the preferred action so a "resume next" would be required.
>
>>From Francesco Balena's "Programming Microsoft Visual Basic.NET" (p.
>151-152):
>
>"The ability to mix the old-style error trapping and the structured 
>exception handling mechanism is
>important. Whereas the latter approach makes for a more streamilined way of 
>dealing with errors, you
>shouldn't automatically assume that it is also the best way to deal with 
>errors in all circumstances. As a
>matter of fact, you might argue that structured error handling is LESS 
>flexible than the mechanisms
>based on the time honored On Error GoTo statement because there is no 
>simple way to duplicate the
>functionality of Resume and Resume Next statements using the 
>Try...Catch...Finally block. If you need
>to reexecute the statement that caused the problem or just ignore it and 
>skip to the statement that
>follows, using On Error might simplify your coding noticeably. The bottom 
>line: use the technique that
>suits the problem at hand...."
>
>So, there is no simple replacement for "Resume Next" in .Net, you can 
>mix-and-match On Error Go To
>stuff and structured exception handling, but - here's the kicker -  a 
>routine cannot contain BOTH a
>'Try' statement and an 'On Error' or 'Resume' statement. So if you have a 
>situation where you need to
>use 'Resume Next' syntax and you also want to use 'Try' syntax, the only 
>method I've discovered to
>date is to call out to another procedure that uses the old-style error 
>handling from the Try block. The
>VB Err object has been extended in VB.NET with a GetException method that 
>is used to Throw an
>exception back to the calling Try block. I modified my console app below 
>and added Sub LoadCollection
>to demonstrate:
>
>Module Module1
>
>   ' Declare module-level collection.
>   Private mCollection As New Collection()
>
>      Sub Main()
>
>      'Uncomment next line to test for NullReferenceException()
>      'mCollection = Nothing
>
>      Try
>         Call LoadCollection()
>
>         'Uncomment next line to test for "Some other exception found: "
>         'Console.WriteLine(mCollection(4)) ' IndexOutOfRangeException
>
>         'Multiple catch clauses ending with "catch all" to ensure all
>         'exceptions are handled.
>      Catch exNRF As NullReferenceException
>         Console.WriteLine("Collection does not exist")
>      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.Write(mCollection.Count)
>
>      Console.WriteLine("")
>      Console.WriteLine(">>> Press Enter to terminate the program <<<")
>      Console.ReadLine()
>
>      End Sub
>
>      Sub LoadCollection()
>
>         Dim oObject1 As New Object()
>         Dim oObject2 As New Object()
>         Dim oObject3 As New Object()
>
>         ' OldStyleErrorHandler vs. Structured Exception Handling
>         On Error GoTo ErrorHandler
>
>         'Assign objects to module-level collection.
>         mCollection.Add(oObject1, "One")
>         mCollection.Add(oObject2, "Two")
>         mCollection.Add(oObject3, "Three")
>      'Uncomment the next line to test Resume Next.
>      'mCollection.Add(oObject1, "Three") ' duplicate key value
>
>ErrorHandler:
>      Select Case Err.Number
>         Case 91 ' Object referenc not set to an instace of an object.
>            Console.WriteLine(Err.Number & " " & Err.Description)
>            Throw Err.GetException 'Throws NullReferenceException back to 
>Try block.
>         Case 457 ' Add failed. Duplicatekey value supplied.
>            Console.WriteLine(Err.Number & " " & Err.Description)
>            Resume Next
>      End Select
>
>   End Sub
>
>End Module
>
>
>P.S. The Finally statment is a great place for clean up code because the 
>code between the Finally
>keyword and the End Try keywords is guaranteed to run whether or not the 
>code in the Try block
>threw an exception.
>
>HTH
>
>Bob
>
>
>
>>From: <jcolby at colbyconsulting.com>
>>Reply-To: Access Developers discussion and problem 
>>solving<accessd at databaseadvisors.com>
>>To: "Access Developers discussion and problem 
>>solving"<accessd at databaseadvisors.com>
>>Subject: RE: [AccessD] OT: VB.Net - general questions
>>Date: Mon, 14 Jul 2003 17:18:45 -0400
>>
>>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
>>
>>
>>_______________________________________________
>>AccessD mailing list
>>AccessD at databaseadvisors.com
>>http://databaseadvisors.com/mailman/listinfo/accessd
>>Website: http://www.databaseadvisors.com
>
>
>
>>From: <jcolby at colbyconsulting.com>
>>Reply-To: Access Developers discussion and problem 
>>solving<accessd at databaseadvisors.com>
>>To: "Access Developers discussion and problem 
>>solving"<accessd at databaseadvisors.com>
>>Subject: RE: [AccessD] OT: VB.Net - general questions
>>Date: Mon, 14 Jul 2003 17:18:45 -0400
>>
>>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
>>
>>
>>_______________________________________________
>>AccessD mailing list
>>AccessD at databaseadvisors.com
>>http://databaseadvisors.com/mailman/listinfo/accessd
>>Website: http://www.databaseadvisors.com
>
>_________________________________________________________________
>Add photos to your messages with MSN 8. Get 2 months FREE*.  
>http://join.msn.com/?page=features/featuredemail
>
>_______________________________________________
>AccessD mailing list
>AccessD at databaseadvisors.com
>http://databaseadvisors.com/mailman/listinfo/accessd
>Website: http://www.databaseadvisors.com

_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online  
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963



More information about the AccessD mailing list