+ Reply to Thread
Results 1 to 7 of 7

Error propagation

  1. #1
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Error propagation

    Hello

    I have multiple procedures A, B, C, etc.

    A calls B and B calls C.

    I define error handlers in all my procedures as this:-

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.
    I was hoping that if there is an error in procedure B, i will get the message "FATAL ERROR in B!"

    This will help me in quick debugging of errors.

    But, it seems error gets propagated to calling procedure, and I always end up getting the error message defined in first procedure: "FATAL ERROR in A!"

    Is there a way for error message to be returned from the procedure where the error occured?

    Thanks
    Ajay

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,003

    Re: Error propagation

    Not sure what you mean.

    Please Login or Register  to view this content.

    Gets both error messages.

    We probably need a more realistic example.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,219

    Re: Error propagation

    Hi ajay,

    How about having a single GLOBAL VARIABLE that is changed to the procedure name the first line the procedure starts. Then if there is an error you call the single error routine and it will display the current value of the error variable which is the name of the Sub being run.

    See http://www.ozgrid.com/VBA/variable-scope-lifetime.htm for more on global variables.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Error propagation

    This is what I am trying to do.
    And you are right. In this simplistic example, it works fine.
    I guess I need to look at my code again, and see what am I missing.

    Thanks

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    03-11-2010
    Location
    India
    MS-Off Ver
    2010
    Posts
    268

    Re: Error propagation

    Quote Originally Posted by MarvinP View Post
    Hi ajay,

    How about having a single GLOBAL VARIABLE that is changed to the procedure name the first line the procedure starts. Then if there is an error you call the single error routine and it will display the current value of the error variable which is the name of the Sub being run.

    See http://www.ozgrid.com/VBA/variable-scope-lifetime.htm for more on global variables.
    Thanks, Marvin!
    If I can't figure out why my code is not working, I will go for this solution.

    Thanks
    Ajay

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,219

    Re: Error propagation

    When things aren't like you think, you are thinking something wrong!!

    I think you think "Exit Sub" does exactly the same thing as "End Sub" and I'm not sure that is true. I'd have to check that assumption somehow.....

    I also wonder when you do your first
    On Error GoTo ErrHandler the code points to the ErrHandler that is in your Sub A. Because you don't EXIT SUB it still knows where that is and keeps waiting for an error...

    How about changing the name of ErrHandler for each Sub...
    Like ErrHandlerA, ErrHandlerB, ErrHandlerC and see what happens.

    Overcoming each of these unknown problems makes you a better programmer.

    I think the answer is hiding in this site:
    http://www.cpearson.com/Excel/Scope.aspx Read it and learn.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,003

    Re: Error propagation

    Your code, as repeated here (with a slight mod: dimming z) works for me as expected.

    Please Login or Register  to view this content.

    AFAIK, the label (ErrHandler) is local to each subroutine. And, although subroutine A calls subroutine B, and subroutine B calls subroutine C, each subroutine only knows about its own error handler label. If I change the label in subroutine C to something else, I will get a compile error so clearly it's not happy to accept a previously defined label.

    Why not share the "real" code?

    Regards, TMS

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Auto Propagation
    By ahssrob in forum Excel General
    Replies: 4
    Last Post: 06-11-2022, 10:32 AM
  2. [SOLVED] Propagation of values in a spreadsheet
    By Askalian in forum Excel General
    Replies: 4
    Last Post: 02-14-2013, 07:39 PM
  3. Row insertion with downward propagation
    By elbrus186 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2012, 04:47 PM
  4. Replies: 6
    Last Post: 01-09-2011, 01:50 AM
  5. data propagation
    By HelenT in forum Excel General
    Replies: 0
    Last Post: 03-22-2005, 12:34 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1