VB一个主程序过程A中有子过程B,B中又有个C,在C中有错误处理,如有出错,则ABC全跳出,结束当前主程序,如何处理啊,exit只能是当前,goto又不能在过程外设置标号,请教高手。

解决方案 »

  1.   

    如果是结束程序,可以在过程中用end。建议将b,c改成函数,对返回值进行判断是否退出。例:Private sub a()
    '调用过程b
    if b=false then 
    '结束程序
    else
    '继续执行end ifend subprivate function b() as boolean
    on error goto eL:
    '以下完成任务
    b=true
    exit function
    eL:
    b=false
    end function
      

  2.   

    on error goto erflag
    erflag:exit sub
      

  3.   

    on error goto zhidingdifang
    zhidingdifang:下一个程序
      

  4.   

    if   then goto zhidingdifang zhidingdifang:下一个程序
      

  5.   

    同意一楼的观点,楼主的VB主程序过程没有错误处理是很危险的,没有万无一失的代码,建议楼主至少在主过程加上错误处理,只要主控程序有错误处理,则其无论多少层之下的错误都能被捕获到,如果要一次性捕获,前提条件是下面的所有子程序里面没有错误处理。最好的错误处理机制就是函数返回型,如下所示,这样不仅能够捕获错误,还能记录下错误发生的程序调用路径
    Sub Main()
    on error goto errorhandleif not subprocedure1 then goto errorhandleron error goto 0
    Exitpnt:
    '代码清理
    exit suberrorhandle:
    '在文本文件记下错误或是使用MSGBOX提示错误
    GOTO Exitpnt
    END SUBFunction subprocedure1
    on error goto errorhandleif not subprocedure2 then goto errorhandleron error goto 0
    Exitpnt:
    '代码清理
    exit suberrorhandle:
    '在文本文件记下错误
    GOTO Exitpnt
    end functionFunction subprocedure2()
    on error goto errorhandledebug.print 1/0on error goto 0
    Exitpnt:
    '代码清理
    exit suberrorhandle:
    '在文本文件记下错误
    GOTO Exitpnt
    end function