| 在VB 6.0中,争议最多的就是对错误的处理。
原有的On Error Goto语句的错误处理并不完善,有时,还影响了应用程序的开发与维护。
而在VB.NET中提供了另外一种处理语句:Try…Catch…Finally结构。Try语句监视在Try与第一个Catch之间的代码,其中内嵌了Resume Catch语句用来过滤错误;Finally是整体的执行。为了保持向下兼容,VB.NET依然支持On Error Goto语句。下面两段代码展示了这两种处理方法的区别。 一、使用On Error Goto语句处理错误
fReRaise=False On Error Goto Errhandler <假如这里就是发生异常错误的代码> Goto C1eanuP Errhandler If Conditior]WeCanHandle Then <error handling code> else fReRaise=True End if
C1eanUp <clean up> if fReRaise Then err.Raise err
二、使用Try…Catch…Finally结构处理错误
Try <code that maY fail> Catch ConditionWecanHandle <error bandlina code>
FinallY <clean uD> End Try |