CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2004
    Posts
    130

    Errorhandler query

    Hi All,

    I have a small problem

    I have a series of actions each opening a file for input. If the file doesnt exist I get an err.Number 53

    An error handler line points to an error handler that creates the file.

    However, the first set of actions works fine but all errorhandler lines afterwards do not get picked up. Any ideas what i have done wrong? The code stops with an error 53 so its not that there is another error number


    On Error GoTo Errorhandler1:

    Perform action

    Errorhandler1:
    If Err.Number = 53 Then Err.Clear: GoTo CreateMyFile1


    'next block of actions
    On Error GoTo Errorhandler2:

    Perform action

    Errorhandler2:
    If Err.Number = 53 Then Err.Clear: GoTo CreateMyFile2

    'next block of actions
    On Error GoTo Errorhandler3:

    Perform action

    Errorhandler3:
    If Err.Number = 53 Then Err.Clear: GoTo CreateMyFile3

  2. #2
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: Errorhandler query

    looking at your example, maybe you would be better off using "ON ERROR RESUME NEXT" and handling the errors in-line.

    other than that, try in errorhandler1, instead of using "goto createmyfile1", use "resume createmyfile1". that would probably fix your problem also, although i would still suggest looking at handling the errors inline

  3. #3
    Join Date
    Jun 2002
    Location
    Clane, Ireland
    Posts
    766

    Re: Errorhandler query

    This maybe totally off the wall, but you do not have an END IF after your IF ERR = 53, so is it possible that everything else is within this IF statement, alternatively if you don't need the END IF, your next line says GOTO which means that you will jump the remainder of the error handling.

    HTH
    JP

    Please remember to rate all postings.

  4. #4
    Join Date
    Jan 2002
    Location
    Quebec/Canada
    Posts
    124

    Re: Errorhandler query

    I think your problem is with the Goto CreateMyFile1 as vb think the process is still processing an error in Errorhandler1. You should use a Resume or Resume Nest instead of Goto CreateMyFile1 as Zeb said. I think this would solve your problem.

    also when dealing with files, I always check if the file exists first, then open it

    Code:
    if (dir(sSomeFile) = "") then 
      call pCreateFile()
    end if 
    open sSomeFile
    this is much better then using error handling and Gotos (in my opinion)

  5. #5
    Join Date
    Feb 2004
    Posts
    130

    Re: Errorhandler query

    Thanks for your suggestions chaps. Most grateful. I will have a play over the weekend and see where I get.

    My code as it stands is very messy. The CreateMyFile is a placeholder in the procedure that sits above the errorhandler line for each section so the remainder of the code does get read. Absolute nightmare so I am not surprised that it mess's up.

    Thanks again

  6. #6
    Join Date
    Oct 2001
    Location
    Melbourne, Australia
    Posts
    576

    Re: Errorhandler query

    Quote Originally Posted by jp140768
    This maybe totally off the wall, but you do not have an END IF after your IF ERR = 53, so is it possible that everything else is within this IF statement, alternatively if you don't need the END IF, your next line says GOTO which means that you will jump the remainder of the error handling.

    HTH
    Actually, there are two forms to IF. you can have the multiline version, as in:
    Code:
    IF condition THEN
       ...
    ELSE
       ...
    END IF
    or you can have a single line version, which doesn't require an endif, and where multiple statements are seperated by a colon (
    Code:
    IF condition THEN statement1 : statement2
    the second version is good for samples like GEM_MAN's because it is really only one statement (technically 2, but you get my drift) and it is far more readable as the one line then as the 4 lines it would take using the first (more common) form of IF

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured