linux社区爱心援助Linux认证系列教程业界动态站务新闻公司招聘网络学院网址大全LPI专题CISCO专题
设为首页
加入收藏
管理团队
JSP  
JAVA  
PERL  
 您的位置:首页 > 开发语言 > Delphi >
栏目导栏
  php
  JSP
  ASP
  asp.net
  JAVA
  c/c++/c#
  perl
  JavaScript
  Basic
  Delphi
资料搜索
热门文章
·HexToStr函数和StrToHex函数
·Delphi中的进制转换
·delphi轻松设置无边框透明窗体
·delphi第三方控件安装(Ehlib)
·DELPHI组件安装全攻略
·delphi动态创建控件
·开发工具比较Visual C++ VS De
·delphi完整身份证效验程序实例
·delphi中的Format函数详解
·delphi编程获取打印机的打印任
·如何判断文本文件的编码格式
·深入研究Variant数组
·用delphi编写网络游戏的外挂
·Delphi技巧-用户自定义数据类型
·delphi设置控件透明
最新文章
·在应用程序中跟踪MOUSE的坐标
·压缩和修复MS Access 2000文件
·DELPHI 6.0 动画制做
·怎样在Delphi中调用LastError信
·怎样得到主域服务器名称
·怎样利用递归实现删除某一目录
·读出主键下所有项
·如何制作照片底片效果的图像(
·获得Modem的状态
·WebBrowser屏幕滚动的实现,设
·有关字符串处理的小技巧
·Delphi建立键盘鼠标动作纪录与
·Delphi中布尔类型辨析
·DELPHI程序注册码设计
·图形的不规则的Copy
Google
 
Delphi中异常的处理
[ 作者:  加入时间:2007-12-03 14:50:54  来自:Linux联盟收集整理 ]
今天我们将讨论两个题目:gwuLinux联盟
一、 异常的理论gwuLinux联盟
二、 异常的简单处理方法gwuLinux联盟
gwuLinux联盟
异常的理论gwuLinux联盟
利用异常,我们可以处理指定代码中的特定区域的错误。在Delphi中,如果发现了异常,而你没有明确的处理它,那么一个默认的异常处理程序将处理该异常。如果我们对Delphi有些编程经验的话,我们就会发现Delphi生成的整个程式就是一个大的Try…Except块。Delphi附带了许多内置异常类系列,用于处理大范围的错误条件。我们可以很容易的创建自己的异常类,来处理程序中易于受错误影响的关键事件。以下是Delphi在SysUtils中声明的异常处理的基类:gwuLinux联盟
  Exception = class(TObject)gwuLinux联盟
  privategwuLinux联盟
    FMessage: string;gwuLinux联盟
    FHelpContext: Integer;gwuLinux联盟
  publicgwuLinux联盟
    constructor Create(const Msg: string);gwuLinux联盟
    constructor CreateFmt(const Msg: string; const Args: array of const);gwuLinux联盟
    constructor CreateRes(Ident: Integer); overload;gwuLinux联盟
    constructor CreateRes(ResStringRec: PResStringRec); overload;gwuLinux联盟
    constructor CreateResFmt(Ident: Integer; const Args: array of const); overload;gwuLinux联盟
    constructor CreateResFmt(ResStringRec: PResStringRec; const Args: array of const); overload;gwuLinux联盟
    constructor CreateHelp(const Msg: string; AHelpContext: Integer);gwuLinux联盟
    constructor CreateFmtHelp(const Msg: string; const Args: array of const;gwuLinux联盟
      AHelpContext: Integer);gwuLinux联盟
    constructor CreateResHelp(Ident: Integer; AHelpContext: Integer); overload;gwuLinux联盟
    constructor CreateResHelp(ResStringRec: PResStringRec; AHelpContext: Integer); overload;gwuLinux联盟
    constructor CreateResFmtHelp(ResStringRec: PResStringRec; const Args: array of const;gwuLinux联盟
      AHelpContext: Integer); overload;gwuLinux联盟
    constructor CreateResFmtHelp(Ident: Integer; const Args: array of const;gwuLinux联盟
      AHelpContext: Integer); overload;gwuLinux联盟
    property HelpContext: Integer read FHelpContext write FHelpContext;gwuLinux联盟
    property Message: string read FMessage write FMessage;gwuLinux联盟
  end;gwuLinux联盟
以下内置的异常都可以直接从SysUtils.pas中引用:gwuLinux联盟
EAbort = class(Exception);gwuLinux联盟
gwuLinux联盟
  EHeapException = class(Exception)gwuLinux联盟
  privategwuLinux联盟
    AllowFree: Boolean;gwuLinux联盟
  publicgwuLinux联盟
    procedure FreeInstance; override;gwuLinux联盟
  end;gwuLinux联盟
gwuLinux联盟
  EOutOfMemory = class(EHeapException);gwuLinux联盟
gwuLinux联盟
  EInOutError = class(Exception)gwuLinux联盟
  publicgwuLinux联盟
    ErrorCode: Integer;gwuLinux联盟
  end;gwuLinux联盟
gwuLinux联盟
{$IFDEF MSWINDOWS}gwuLinux联盟
  PExceptionRecord = ^TExceptionRecord;gwuLinux联盟
  TExceptionRecord = recordgwuLinux联盟
    ExceptionCode: Cardinal;gwuLinux联盟
    ExceptionFlags: Cardinal;gwuLinux联盟
    ExceptionRecord: PExceptionRecord;gwuLinux联盟
    ExceptionAddress: Pointer;gwuLinux联盟
    NumberParameters: Cardinal;gwuLinux联盟
    ExceptionInformation: array[0..14] of Cardinal;gwuLinux联盟
  end;gwuLinux联盟
{$ENDIF}gwuLinux联盟
gwuLinux联盟
  EExternal = class(Exception)gwuLinux联盟
  publicgwuLinux联盟
{$IFDEF MSWINDOWS}gwuLinux联盟
    ExceptionRecord: PExceptionRecord platform;gwuLinux联盟
{$ENDIF}gwuLinux联盟
{$IFDEF LINUX}gwuLinux联盟
    ExceptionAddress: LongWord platform;gwuLinux联盟
    AccessAddress: LongWord platform;gwuLinux联盟
    SignalNumber: Integer platform;gwuLinux联盟
{$ENDIF}gwuLinux联盟
  end;gwuLinux联盟
gwuLinux联盟
  EExternalException = class(EExternal);gwuLinux联盟
gwuLinux联盟
  EIntError = class(EExternal);gwuLinux联盟
  EDivByZero = class(EIntError);gwuLinux联盟
  ERangeError = class(EIntError);gwuLinux联盟
  EIntOverflow = class(EIntError);gwuLinux联盟
gwuLinux联盟
  EMathError = class(EExternal);gwuLinux联盟
  EInvalidOp = class(EMathError);gwuLinux联盟
  EZeroDivide = class(EMathError);gwuLinux联盟
  EOverflow = class(EMathError);gwuLinux联盟
  EUnderflow = class(EMathError);gwuLinux联盟
gwuLinux联盟
  EInvalidPointer = class(EHeapException);gwuLinux联盟
gwuLinux联盟
  EInvalidCast = class(Exception);gwuLinux联盟
gwuLinux联盟
  EConvertError = class(Exception);gwuLinux联盟
gwuLinux联盟
  EAccessViolation = class(EExternal);gwuLinux联盟
  EPrivilege = class(EExternal);gwuLinux联盟
  EStackOverflow = class(EExternal)gwuLinux联盟
    end deprecated;gwuLinux联盟
  EControlC = class(EExternal);gwuLinux联盟
{$IFDEF LINUX}gwuLinux联盟
  EQuit = class(EExternal) end platform;gwuLinux联盟
{$ENDIF}gwuLinux联盟
gwuLinux联盟
  EVariantError = class(Exception);gwuLinux联盟
gwuLinux联盟
  EPropReadOnly = class(Exception);gwuLinux联盟
  EPropWriteOnly = class(Exception);gwuLinux联盟
gwuLinux联盟
  EAssertionFailed = class(Exception);gwuLinux联盟
gwuLinux联盟
{$IFNDEF PC_MAPPED_EXCEPTIONS}gwuLinux联盟
  EAbstractError = class(Exception) end platform;gwuLinux联盟
{$ENDIF}gwuLinux联盟
gwuLinux联盟
  EIntfCastError = class(Exception);gwuLinux联盟
gwuLinux联盟
  EInvalidContainer = class(Exception);gwuLinux联盟
  EInvalidInsert = class(Exception);gwuLinux联盟
gwuLinux联盟
  EPackageError = class(Exception);gwuLinux联盟
gwuLinux联盟
  EOSError = class(Exception)gwuLinux联盟
  publicgwuLinux联盟
    ErrorCode: DWORD;gwuLinux联盟
  end;gwuLinux联盟
{$IFDEF MSWINDOWS}gwuLinux联盟
  EWin32Error = class(EOSError)gwuLinux联盟
  end deprecated;gwuLinux联盟
{$ENDIF}gwuLinux联盟
gwuLinux联盟
  ESafecallException = class(Exception);gwuLinux联盟
更详细的异常类大家可以参考在线帮助中的Include/VCL目录里面的资源文件。gwuLinux联盟
我把一个用于演示异常的示例放在了网上(下载地址:http://vip.6to23.com/liebao/exception.zip),这个程式包含了4个可以引发异常的按钮。gwuLinux联盟
我们通常把代码放在Try保留字的后面,将发现错误时引发的代码放在Exception关键字的后面。在示中有这样一段程式:gwuLinux联盟
procedure TForm1.UserExceptClick(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  i, j, k: Integer;gwuLinux联盟
begingwuLinux联盟
  j := 0;gwuLinux联盟
  trygwuLinux联盟
    k := i div j;gwuLinux联盟
  exceptgwuLinux联盟
    on EDivByZero dogwuLinux联盟
      MessageDlg('Aw shucks! If my Momma could see me now!', mtError,[mbOk],0);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
在理解了上面这个简单的try…except代码后,我们还可以尝试着把由Delphi报告的错误与自己定义的错误信息结合起来:gwuLinux联盟
procedure TForm1.DeclareClick(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  i: Integer;gwuLinux联盟
  S, S1: string;gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    i := StrToInt('Sam');gwuLinux联盟
    ShowMessage(IntToStr(i));gwuLinux联盟
  exceptgwuLinux联盟
    on E:EConvertError do begingwuLinux联盟
      S := ('Class where error occurred: ' + Self.ClassName);gwuLinux联盟
      S1 := 'Type of error: ' + E.ClassName;gwuLinux联盟
      MessageDlg(S + #13#10 + S1 + #13#10 + E.Message, mtError, [mbOk], 0);gwuLinux联盟
    end;gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
有时候我们还希望显示异常引发的实际位置。ExceptAddr函数可以返回这个地址:gwuLinux联盟
procedure TForm1.CalcAddClick(Sender: TObject);gwuLinux联盟
constgwuLinux联盟
  CR = #13#10;gwuLinux联盟
vargwuLinux联盟
  i, j, k: Integer;gwuLinux联盟
begingwuLinux联盟
  j := 0;gwuLinux联盟
  trygwuLinux联盟
    k := i div j;gwuLinux联盟
  exceptgwuLinux联盟
    on E:EDivByZero dogwuLinux联盟
       MessageDlg(E.Message + CR + 'MyCalc: $' + Address2Str(ExceptAddr),gwuLinux联盟
         mtError, [mbOk], 0);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
通过上面的讲解,我们看到了Delphi处理异常竟是如此的简单!异常可以帮助我们避免发生错误之后意外的执行任何敏感的代码。gwuLinux联盟
明天我们将讨论“引发异常”和“创建及引发自己的异常”。如对本节有问题,请随时到http://ilovedelphi.i99.cc(天极网Delphi讨论区的转址URL)提出,我们的版主会很快解答你的提问!gwuLinux联盟
如果你在本周结束“异常”讨论时仍有关于它的疑问,那么天极网Delphi讨论区随时欢迎你的提问!gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
今天天我们将讨论“引发异常”。在开始讨论之前,请先下载(http://vip.6to23.com/liebao/ExceptTypes.zip)。gwuLinux联盟
可以引发的最简单的对象类型是VCL Exception类。Object Pascal与其他与语言在处理异常上有所不同,它不能引发具有诸如Integer或String之类的简单类型的异常,因为引发一个对象而不是一个简单的整型更为有用,其优点当然是对象可以同你进行对话,而且可以进行查询,而简单的类型则没有这个功能。如果将整数作为异常发出,实际上只是对从函数中返回整型作为错误代码的方式进行了很小的改进。请大家考虑下面这个Procedure:gwuLinux联盟
procedure TForm1.ExceptionClassBtnClick(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  i: Integer;gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    i := StrToInt('Sam');gwuLinux联盟
    ShowMessage(IntToStr(i));gwuLinux联盟
  exceptgwuLinux联盟
    on A:Exception dogwuLinux联盟
      ShowMessage('Info: ' + A.Message);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
如果发生错误,StrToInt中的代码将引发异常。如果它没有明确使用raise关键字,那么就不会发生异常。异常并不神秘,发生异常是因为在代码中使用了raise表达式,如Delphi中StrToInt函数的代码所示:gwuLinux联盟
function StrToInt(const s:string):IntegergwuLinux联盟
constgwuLinux联盟
  s=’Could not cnvert string. Trouble with character: %d’;gwuLinux联盟
vargwuLinux联盟
  E:Integer;gwuLinux联盟
BegingwuLinux联盟
  Val(S,Result,E);gwuLinux联盟
  If E<>0 thengwuLinux联盟
Raise EconvertError.Create(Format(S,E));gwuLinux联盟
End;gwuLinux联盟
上例中使用了raise关键字,我们将在后面的“异常”讨论中涉及之。gwuLinux联盟
下面这段代码说明了如何引发一个VCL异常:gwuLinux联盟
procedure TForm1.ThrowVCLExceptionClick(Sender: TObject);gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    raise Exception.Create('VCL class');gwuLinux联盟
  exceptgwuLinux联盟
    on E:Exception dogwuLinux联盟
      ShowMessage(E.ClassName + ' ' + E.Message);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
这段代码自动创建Exception类的一个例子,并用一个简单的字符串调用它的构造函数。我们还可以通过使用下列的Exception类的构造函数通过数字自动从表中取回一项:gwuLinux联盟
constructor CreateRes(Ident:Integer;Dummy:Extended=0);gwuLinux联盟
其中的Ident是连接到应用程序的资源的字符串的ID。Dummy是一个可以完全忽略的默认参数,加上它的原因只是为了向C++提供它的构造函数的唯一标识。gwuLinux联盟
昨天我们向大家展示了SysUtils.pas中的一些VCL异常类的名字,而所有的这些类都有是从一个叫做Exception的父类中派生出来的。Delphi所做的最伟大的事情之一就是类分层。gwuLinux联盟
假设我们不知道引发错误类的确切名字,我们可以尝试用下面这段代码检查它的ClassName:gwuLinux联盟
exceptgwuLinux联盟
  on E:Exception do begingwuLinux联盟
     S:=Self.ClassName;gwuLinux联盟
     S1:=E.ClassName;gwuLinux联盟
     MessageDlg(S+#13#10+S1,mtError,[mbOK],0);gwuLinux联盟
     End;gwuLinux联盟
End;gwuLinux联盟
这段代码报告了发生异常的类。gwuLinux联盟
现在请大家考虑下列代码:gwuLinux联盟
procedure TForm1.MultiCatchBtnClick(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  i: Integer;gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    i := StrToInt('Sam');gwuLinux联盟
    ShowMessage(IntToStr(i));gwuLinux联盟
  exceptgwuLinux联盟
    on C:EDatabaseError dogwuLinux联盟
      ShowMessage(C.Message);gwuLinux联盟
gwuLinux联盟
    on E:EDivByZero dogwuLinux联盟
      ShowMessage(E.Message);gwuLinux联盟
gwuLinux联盟
    on A:Exception dogwuLinux联盟
      ShowMessage('Exception: ' + A.Message);gwuLinux联盟
gwuLinux联盟
    on B:EConvertError dogwuLinux联盟
      ShowMessage('EConvertError: ' + B.Message);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
我们可以从代码中看出,错误发生在I=StrToInt(‘Sam’);很明显的,程序会自己跳过gwuLinux联盟
on C:EDatabaseError dogwuLinux联盟
      ShowMessage(C.Message);gwuLinux联盟
gwuLinux联盟
    on E:EDivByZero dogwuLinux联盟
      ShowMessage(E.Message);gwuLinux联盟
因为引发的异常如这两种错误类型无关。或许大家会认为代码将跳过except块而直接执行EconvertError异常,但事实却是相反的,由于多态性的规则使得EconvertError可以被指定它的一个父对象,所以给出了这个信息之后,在方法中安排代码的逻辑方式应该如下:gwuLinux联盟
EdatabaseErrorgwuLinux联盟
EdivByZerogwuLinux联盟
EconvertErrorgwuLinux联盟
ExceptiongwuLinux联盟
这样,如果Delphi发现前面三个语句匹配失败,最后将用Exception语句处理。gwuLinux联盟
我们还可以采取一些措施来大范围的获取异常:gwuLinux联盟
procedure TForm1.GenericCatchBtnClick(Sender: TObject);gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    StrToInt('Sam');gwuLinux联盟
  exceptgwuLinux联盟
    on E:Exception dogwuLinux联盟
      ShowMessage('Class: ' + E.ClassName + 'Info: ' + E.Message);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
这段代码展示了except块将最大范围的捕获异常。gwuLinux联盟
“异常”的基础理论我们就讨论到这里。明天我们将讨论“创建和引发自己的异常”。如果就讨论有任何疑问,请即至http://ilovedelphi.i99.cc(天极网Delphi讨论区的转址URL)提出,我们的版主会很快解答你的提问!gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
今天我们谈谈“创建和引发自己的异常”gwuLinux联盟
请各位先下载http://vip.6to23.com/liebao/MyExcept.zip  这段程式中有三个按钮,有两个都会引发一个异常,第三个按钮不会引发异常,除非你在Edit中输入拼错的“occur”的过去式。gwuLinux联盟
异常之所以发生,是因为它们被明确的引发。让我们再看一遍前边提到的StoToInt函数的代码:gwuLinux联盟
function StrToInt(const s:string):IntegergwuLinux联盟
constgwuLinux联盟
  s=’Could not cnvert string. Trouble with character: %d’;gwuLinux联盟
vargwuLinux联盟
  E:Integer;gwuLinux联盟
BegingwuLinux联盟
  Val(S,Result,E);gwuLinux联盟
  If E<>0 thengwuLinux联盟
Raise EconvertError.Create(Format(S,E));gwuLinux联盟
End;gwuLinux联盟
代码中的EconvertError是一个内置Delphi类型,是在转换过程中发生错误的情况下使用的。在Delphi中,同有什么迫使我们在特定的情况下使用一个特定的异常类,比如,在我们的代码中就引发了EreadError:gwuLinux联盟
procedure TForm1.RaiseEReadClick(Sender: TObject);gwuLinux联盟
begingwuLinux联盟
  raise EReadError.Create('EReadError has occurred');gwuLinux联盟
end;gwuLinux联盟
从上面的代码中我们可以看到,当使用raise然后构造特定异常类型时,就会引发异常。Delphi内置的许多异常类经常会对我们有用,但是在许多时候,我们也需要创建自己的异常类,为了实现这一目的,需要首先声明一个新的类,例如:gwuLinux联盟
EsillySpellingError=Class(Exception);gwuLinux联盟
这行代码声明了由Exception派生的EsillySpellingError类。在程序我是这样实现的:gwuLinux联盟
procedure TForm1.EReadAddrClick(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  S: string;gwuLinux联盟
begingwuLinux联盟
  S := Edit1.Text;gwuLinux联盟
  if UpperCase(S) = 'OCCURED' then begingwuLinux联盟
    S := 'A Silly Spelling error has occured! ' +gwuLinux联盟
         'The correct spelling is occuRRed, not occuRed: ';gwuLinux联盟
    raise ESillySpellingError.Create(S + GetAddr);gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
同样的,我们可以用前面提到过的Except引用这段错误代码:gwuLinux联盟
TrygwuLinux联盟
……gwuLinux联盟
ExceptgwuLinux联盟
  On E:EinputError dogwuLinux联盟
  Code=E.ErrorCode;gwuLinux联盟
End;gwuLinux联盟
需要注意的是,在try..except块的except中需要明确指明错误的类型。gwuLinux联盟
引发异常最懒的办法是使用下面这行代码:gwuLinux联盟
raise Exception.Create(‘Another lazy man error has occurred!’);gwuLinux联盟
在我们后面将要提到的ResError程式中,我们会更多的了解到如何创建自己的异常类。gwuLinux联盟
明天我们将讨论“再引发异常和try…finally块”。如果就讨论有任何疑问,请即至http://ilovedelphi.i99.cc(天极网Delphi讨论区的转址URL)提出,我们的版主会很快解答你的提问!gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
gwuLinux联盟
今天我们要讨论的是“再引发异常和try…finally块”gwuLinux联盟
请先下载http://vip.6to23.com/liebao/RF.zip,“再引发异常”部分使用了RF.zip包中的RaiseAgain.zip中的代码,“try…finally”部分使用了压缩包中的Final.zip的代码。gwuLinux联盟
再引发异常的关键是使用了raise关键字。下面这段代码演示了raise的用法。gwuLinux联盟
procedure TForm1.Button1Click(Sender: TObject);gwuLinux联盟
vargwuLinux联盟
  K: Integer;gwuLinux联盟
begingwuLinux联盟
  trygwuLinux联盟
    k := StrToInt('Sam');gwuLinux联盟
    ShowMessage(IntToStr(k));gwuLinux联盟
  exceptgwuLinux联盟
    on E:Exception do begingwuLinux联盟
      ShowMessage('Something went wrong');gwuLinux联盟
      raise;gwuLinux联盟
    end;gwuLinux联盟
  end;gwuLinux联盟
end;gwuLinux联盟
再引发异常可以让我们在执行了一些定制处理后再让Delphi自动确切的通知用户出错的位置。gwuLinux联盟
正如我们所了解的,try…except块能使程式的执行指针从发生错误的地方直接跳到处理错误的地方。这一点在大多数情况下都是很好的,但是有时会希望无论情况如何,都能确保执行在错误和异常处理程式之间的代码,try…finally块为我们解决了这个问题。以下代码说明了try…finally块的用法,在运行之间请先将Delphi中的Optimization关掉。gwuLinux联盟
procedure TForm1.RunCalculation;gwuLinux联盟
vargwuLinux联盟
  i,j,k: Integer;gwuLinux联盟
begingwuLinux联盟
  j := 0; i := 3;gwuLinux联盟
  trygwuLinux联盟
    k := i div j;gwuLinux联盟
    Panel1.Caption := 'Sam ' + IntToStr(k); // never gets calledgwuLinux联盟
  finallygwuLinux联盟
    GlobalString := 'This global string set in try..finally block.';gwuLinux联盟
  end;gwuLinux联盟
  GlobalString := 'Toot'; //  never gets called;gwuLinux联盟
end;gwuLinux联盟
这个函数强迫引发了异常,但是是在try…finally块中完成中。在正常情况下,代码指针从引发异常的地方直接跳到处理异常的地方,然而,为了确保GlobalString变量被设为新值,try…finally改变了这个过程。gwuLinux联盟
作为一个规则,try…finally块通常是不太费时间的,应该可以很轻松的将它们分散到代码中,相当的自由哦~ 另一方面,try…except块相当费时间,所以应该谨慎的决定它出现在程式中的频率。我在刚刚接触try…except和try…finally时,有些混淆,不过,一旦自己写一些练手用的代码,就会很容易的搞懂这两个“块”的区别所在,这样才能在实践中正确的使用呀~gwuLinux联盟
我们的Final程式确切的说明了try…finally是如何工作的。然而,给它们的任务通常是保证释放对象的内存。明天我们将讨论“流、异常和空闲空间”,并给大家一个StreamException程式来说明这些问题。 Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·在Delphi中侦测剪贴板的变化  (2007-12-03 14:48:21)
 ·delphi中的快捷键大收集  (2007-12-03 14:44:45)
 ·如何为Delphi程序添加事件和事件处理器  (2007-12-03 14:44:11)
 ·DELPHI中线程的初步探索  (2007-12-03 14:43:37)
 ·delphi端口扫描  (2007-12-03 14:39:06)
 ·DELPHI 中的规则表达式  (2007-12-03 14:37:59)
 ·面向对象编程与Delphi技术浅入讨论  (2007-12-03 14:34:41)
 ·实例讲解如何处理CLOB字段的动态PL/SQL  (2007-11-29 11:28:49)
 ·实例讲解如何处理CLOB字段的动态PL/SQL  (2007-11-29 11:28:49)
 ·ASP的错误处理集锦  (2007-11-28 14:04:11)