Error handling not working

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The error handling here does not seem to handle it.

Put some data in say the first three rows from A1 to D3 for example.

Clearly the array will have only 9 elements and in particular will crash on this line:

Code:
EntryArray.DataArrayItem(10, 1)

Question is why does the error handler not deal with it?

Class1:

Code:
Option ExplicitPrivate pDataArray As Variant
Property Let DataArray(DArray As Variant)
    pDataArray = DArray
End Property
Property Get DataArrayItem(RowIndex As Integer, _
                           ColIndex As Integer) As Variant
    DataArrayItem = pDataArray(RowIndex, ColIndex)
End Property

Module1;

Code:
Option Explicit
Sub Test()
    
    On Error GoTo ErrHandler
    
    Dim EntryArray As Class1
    Set EntryArray = New Class1
    
    Dim a As Variant
    
    EntryArray.DataArray = Cells(1, 1).CurrentRegion.Value
        
    a = EntryArray.DataArrayItem(10, 1)
        
ErrHandler:

    Exit Sub
    
End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
See this Article by Chip Pearson...

Break In Class Module

He says you should set it to Break in Class Module, which is what I did and the eror handling didn't handle it.

If instead I choose Break in unhandled Errors, the error handling does indeed handle it.

So shouldn't the setting be set to Break on unhandled Errors?
 
Last edited:
Upvote 0
The behavior you describe is normal for those two error handling options.

If you want the error handler that was enabled in the calling procedure to handle the error in class module, then you could set the option to (only) "Break on Unhandled Errors".

Chip Pearson's article recommends "Break in Class Module" because it allows the developer to more easily identify the unanticipated errors during development.

Typically, it's better to have a Class provide it's own validation and error handling. That allows the Class to be repurposed without having to rely on the calling procedure and application to do that work.
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top