      5 

        

        ,  ' ,   , 
     "" . 

      

     ,         
   . 

       

        ,     , 
   . 

    begin 
    sStrVal := 'Delphi Rock '; 
    cVal := CharFromString(sStrVal,12); 
    if cVal <> #0 then 
    begin 
    if cVal <> 's' then 
    _if_ InsertChar ( ' s ' , sStrVal, 12) then 
    ShowMessage(sStrVal) else 
    Exit; end 
    else Exit/end; 

      UseFunctions    . 
 CharFromString        0, 
     .  InsertChar  True, 
          ( 
  False).         
 ,   .   , 
 UseFunctions  .    ,  
    "Delphi Rock". 
        CharFromString  InsertChar   , 
  ,   ?    
     ,       
 .    UseFunctions. 

    procedure UseFunctions; var cVal: char; 
    sStrVal: string; begin try 
    sStrVal := 'Delphi Rock '; 
    cVal := CharFromString(sStrVal,12); 
    if cVal <> 's' then 
    InsertChar('s',sStrVal,12) ; ShowMessage(sStrVal); except Expend; end; 

        CharFromString  InsertChar  
  ,     ,  
      .   ,  
    Exit.  ,    
 RTL Delphi    ,   "" 
  . -,  Delphi    
,          . 
 ,   Delphi     
  (Application Programming Interface API)  OS, 
     .     Delphi  
   Windows 95  NT,   
 ,    OS     
.          
    . 

       

           , 
     .     
 .       ,  
        .    
  . 
        ,   . 
          ,    SQL,   BDE  ODBC. 
         . 
         ,     
 Unimodem. 

     5.    105 

         ,     . 
          RTL  VCL Delphi. 
       , ,    , 
   .   ,  ,   
 .       
"" .        
,   . 
           ,  
.   -     ( 
) ,    -*      
  . ,>                                                                                                                         
            , 
    New (), GetMem ()  HeapAlloc (),    
 ""   ,   .  
         
  . 
     ""       ,   
    . 
    1.        try. . . except. 
    2.         . 
        ,     
 .      
 .      ,   
    ,     ,   
   ,    
       .  ,    , 
     ,      .  
       
,        . 
    ,     try. . .except, ,   
 try. . . finally.  ,      
    .  ,   
   .      
      . 

          

       Delphi,   VCL,    
Application.    TApplication,    Forms  
      .     
 Delphi,  TApplication     ,    
    . 
         Forms     
Controls,    Uses  Forms.    
Controls   InitControls,    
Application.   ,     
    Application;      
Uses  Forms. 
     Application   .      
      . , 
  ,  Application   OnExcegtion. 
    ~"~ ~ 
     ~ 
    ' ~~ 

     OnException 

    , ,     Application  
  DPR-.      
    Initialize.     
      Automation OLE 
(Object Linking and Embedding    

    106  I.   

     )  ,   OLE.  
  OLE  Automation    18, "  OLE,  
    ActiveX".  __ , 
 onepaTjpjj\jojaw_y.3ajTHTbt  Initialize   
CreateForms,    "$1      
Application.     Application   
?       OnException. 
      -     , 
 Application   HandleException (, ,  
     ).  
-dleException,   ,   ,   
OnException.       ,   
ShowException.        
,    OnEcxeption. 
        OnEcxeption ,     
.       TExceptionEvent.  
 Forms     : 
    TExceptionEvent  = procedure   (Sender:   TObject;   EInstance:   
Exception)   of object;  ,  TExceptionEvent   , 
 . 

      Application.OnException   

         OnEcxeption  Application 
 ,    . -, 
  .  ,     
    DPR- .   DPR- 
    Project Source  View.   , 
   5.1     ,    
  ,   \UDELPHI3\CHP5\. 

     5.1. \UDELPHI3\CHP5\PROJECT1. DPR.   
    OnException  DPR- 

    program Project!; uses 
    SysUtils, 
    Forms, 
    Unitl in 'Unitl.pas' {Forml}; 
    type 
    TGlobalExHandler = class public 
    procedure HandlerProc(Sender: TObject; EInstance: Exception); end; 
    procedure TGlobalExHandler.HandlerProc(Sender: TObject; EInstance: 
Exception); begin 
    {    .} end; 
    var 
    GlobalExHandler: TGlobalExHandler; 
    {$R *.RES} 
    begin 
    GlobalExHandler := TGlobalExHandler.Create; 
    Application.OnException := GlobalExHandler.HandlerProc; 
    Application.Initialize; 
    Application.CreateForm(TForml, Forml); 
    Application.Run; end. 

     5.    107 

       5.1     Uses  
SysUtils.      Exception,    
 TGlobalExHandler    HandlerProc,  
 OnException.    ,  HandlerProc,  ,  
  .     ,   
 . , ,   HandlerProc, 
   .   ,  
    ,    
   ,   ,  ,   
   . 
      HandlerProc     
GlobalExHandler  TGlobalExHandler.     
    DPR-.     
HandlerProc   Application. OnException. 
           ,  
   ,    
 ,       ,    
  HandleException  Application.  ,   , 
  ,   Application.OnEcxeption. 
    Application. OnEcxeption    
GlobalExHandler. HandlerProc.     . 

      

     ,   OnEcxeption,   
 

    TException-Event: TExceptionEvent  =  procedure   (Sender:   TObject; ^                      
    EInstance:   Exception)   of object; 

             
Exception     .    EInstance  
       , 
  public  published.      
     .   
      Message.         
,     , : 
    procedure  TGlobalExHandler.HandlerProc(Sender:   TObject;   EInstance: 
Exception); begin 
    MessageDlg(EInstance.Message,   mtlnformation,    [mbOK],   0); {  
 Uses        Dialogs.} end; 
     Delphi    try    
   ,   .      
     ,      
    . 

       

             
ShowException          
  . .  
    ,    
   ^^-^?^51 . 
          ^_1 
  . ,,      
Delphi    try.      
 ,      ,  
   . 

         IDE 

       (Integrated Development Environment 
IDE) Delphi     .    
  ,      , 
  ,   ,  
     .   ,  
      IDE,   
 ,    . 

    108  I.   

           ,  
.  ,  , ,  -  
 .      ,   
Optimizations ( )   Stack Frames ( ).  
    Compiler    Project Options ( 
    Project^Options). 

      
       Delphi       Fastball 
( ),   .   ,   
,   , .    
^ .    ^ , 
  StdCall  5"51-11       
   . ,        
.  , ""   ,   
  (      ). 

    1^_ ,     , 
    .    ^   
   .    
    ^   
     ,      
,     .    
     <F9>  ,  
  .   o6pa6(3T4jiKajicj<mc^^ 
1^^^212.Й^~9?1^2??2 (   ). 
    1.     IDE   Tools^Options,    
 Environment Options. 
    2.     Preferences. 
    3.     Break On Exception. 

     try 

     Delphi        
.  try. . . finally  try. . .except.     
,    .     
try. . . finally. 

    try 
    { .} finally 
    {  , end; 
    } 

        ,   try  finally,  
 ,    ,  
  finally,        end;. 
         try. . . except.   
 . 

    try 

    { .} except 
    {  .} end; 

        ,    try  except 
  ,   , 
  except  end;. 

      try...finally 

     try. . . finally     
  ,      
  ,      
       
.     1^^^*6" ? 
  ,  ,   . 
        .  
     ,     
   . ,       
   .   , 
       
    .    ,    ,  
        .  
      .  

     5.    109 

           ,  
     ,     .  
   .    ,   
    try". . . finally. 

    procedure TForml.ButtonlClick(Sender: TObject); 
    var pVar: PChar; 
    begin 
    GetMemfpVar, 10); 
    try 
    StrCopy(pVar, '    10 !'); 
    finally 
    FreeMemfpVar, 10); 
    end; end; 

     ,   pVar.     
GetMem  RTL       10 .  
 try  RTL- StrCopy     
  .      10 , 
         .  
  RTL   .   
     EAccessViolation   
 Exception.    ,  
    FreeMem,   , 
   pVar.    FreeMem 
     ,  
     try. . . finally,jio-_    
    try. . .except,   
 . 

      try...exception 

     try. . .exception     
    ,   
   ,     
, _.      
 ,        
  .   on... do  
   . 

    procedure TForml.ButtonlClick(Sender:   TObject); 
    var  iCtr:   integer; 
    begin 
    for  iCtr   := Ord('A')   to  Ord('Z')   do try 
    ChDir(Char(iCtr)+':\DUKE3D'); 
    MessageDlg('\DUKE3D        '+ Char(iCtr), mtlnformation,    
[mbOK],   0); Break; except MessageDlg('\DUKE3D          '+ Char(iCtr), 
    mtError,    [mbOK],   Ob-end; end; 

                Z 
  f or. . . do.       
 ChDir  RTL  ,      
 DUKE3D.        ,  
  ChDir      
EInOutError.    ,    
except.          ,    
   .^-- 
ChDi^_JOJa_pa^ey^e      MessageDlg  
Break ^.   ,       
      . 
        except     
  .  ,   
     .       
  try. . . except  try. . . finally.   
       ,   
       except   . 

    110  I.   


      on...do 

      try. . . except   ,   
          
   ,      ,  
 public  published.     on. . . do. 
     on... do     except  
 end;,    . 

    try 
    (.} except 
    on E:   Exception do 
    {,   ,   .} else 
    {,   ,   .} end; 

     on... do   ,    
    .  ,     
      . 
  . 

    procedure TForml.Button2Click(Sender: TObject); var pVal: PChar; begin try 
    GetMem(pVal,sizeof(Stream.Memory^)); 
    StrLCopy(pVal,Stream.Memory,sizeof(Stream.Memory*)+1); except 
    On E: EOutOfMemory do 
    LowMemTerminate; On E: EAccessViolation do begin 
    if MessageDlg(E.Message*':  ?', 
    mtError,[mbYes,mbNo],0) = mrYes then Application.Terminate end else 
    Raise; end; end; 

         pVal   , 
      Stream.Memory*. 
    StrLCopy       
Stream.Memory*.    ,     
 .      Stream.Memory* 
  GetMem        
EAccessViolation.     ,   
   StrLCopy,      1  
   .      
 EAccessViolation.      . 
          on... do  , 
  case. . .of.    
    EOutOfMemory.    
       ,  
     EAccessViolation.      
  ,  ,     
else.       Raise  
    ("" )    
 Application. 
     ,     On,     
    ,    
   ,   public  published.    
     MessageDlg.   Free  
,     . 
      on... do     
   .   . 

    procedure  TForml.ButtonSClick(Sender:   TObject); 
    var pVal:   PChar; 
    begin try 


     5.    111 

       GetMem(pVal,sizeof(Stream.MemoryA)); 
       StrLCopy(pVal,Stream.Memory,sizeof(Stream.MemoryA)+1); 
    except On E: Exception do begin 
       if E is EOutOfMemory then 
          LowMemTerminate; 
          if E is EAccessViolation 
          then 
            begin 
               if MessageDlg(E.Message+': Terminate the program?', mtError,[mbYes,mbNo],0) = mrYes 
               then 
                 Application.Terminate; 
            end 
          else 
            Raise; 
          end; 
       end; 
     end; 

           On E: Exception,  
   "    ".  
     is      
   .     
           ,  
,    else.  Raise   
    . 

      try 

     Delphi  try     .    ,  
       .    
         try  
finally (except).     . 

    try 
    {} 
    try 
    {} 
    finally 
    {} 
    end; 
    {} except 
    {} end; 

       . 

    try 
    {} 
    try 
    {} 
    except 
    {} 
    end; 
    {} finally 
    {} end; 

        ,    
   .     . 

    try 
    {} try 
    {} except 
    {} 


    112  I.   


    finally 
    {} end; 
    {} 
    end; 

     ,      ,   
  .   5.2     
 try.   try. . . finally   
 try...except. 

     5.2. \UDELPHI3\CHP5\UNIT1. PAS.   try. . . 
finally   try. . . except 

    procedure TForml.Button4Click(Sender:   TObject); var sVar:   string; 
    pVar:   PChar; begin try 
    GetMem(pVar,10); try 
    StrCopy(pVar,'ListBox'); with  ListBoxl.Items  do begin 
    AddCLinel  -  index   0'); Add('Line2  -  index   1'); end; 
    sVar   :=  ListBoxl.Items[2]; 
    {       sVar  pVar.} finally 
    FreeMemfpVar,10); end; except 
    on  EStringListError  do 
    MessageDlg('     ',   mtError,    [mbOk],0); on 
EAccessViolation do 
    MessageDlg('  ',   mtError,[mbOk],   0); else 
    Raise; end; end; 

     ,    5.2,   pVar 
 10  .        StrCopy 
  'ListBox'.        ListBoxl, 
     Items  TListBox     
  sVar.    :    
      ,    Items 
    2, ..  : 

    sVar   :=  ListBoxl.Items[2]; 

        EStringList.   
    try. . . finally,   
FreeMem      .    
 ,   pVar.   
try. . . finally      , 
     ,       
try. . . except.   on. . . do    
 EStringListError,    MessageDlg. 
     ,        
 EAccessViolation, ,    

    sVar   :=  ListBoxl.Items[2]; 

     ,      pVar   
   EAccessViolation. 
           ,  
    GetMem.      
     ?  

     5.    113 

           GetMem  FreeMem?   
 GetMem   -   , 
   EOutOfMemory.  ,   
 finally,     FreeMem.   
     (EInvalidPointe rOperation , 
, EAccessViolation) -    
.    EAccessViolation    
 .     EOutOfResources 
   .     
 else  on. . .do,      
     (       
)    Raise. 
        ,   5.3  try...except 
  try...finally. 

     5.3. \UDELPHI3\CHP5\EXCEPTU1. PAS.   try. . . 
except j   try. .. finally 

    procedure  TForml.ButtonSClick(Sender:   TObject); var  sVar:   string; 
    pVar:   PChar; begin 
    GetMem(pVar,10); try 
    StrCopy(pVar,'ListBox'); 
    (      pVar.   } 
    try 
    sVar := ListBoxl.Items[0]; except 
    on EStringListError do begin 
    ListBoxl.Items.Add('Iteml'); sVar := ListBoxl.Items[0]; end 
    else Raise; end; 
    {    sVar. } finally 
    FreeMem(pVar,10); end; end; 

      5.3   pVar  10  .  
   5.2   ,    , 
   ,   .  
  StrCopy      , 
   pVar.      
    FreeMem,    finally. 
       .  
 StrCopy  ,     
   ListBoxl.  ListBoxl ,    
EStringListError.   on. . . do    ,  
    ListBoxl  ,      
.      FreeMem. 
       

    sVar   :=  ListBoxl.Items[0]; 

      ,      ,    
    .    
 EStringListError.      
      try. . .finally.   
    FreeMem , ,  
 .      
     . 

    114  I.   


       

           Raise.  
,       
 Delphi       Exception  
 . 
    Raise    .      
  InsertChar.      . 

    function  InsertChar(cVal: char; var sVal: string; iPos: integer): boolean; 
    begin 
       Result   :=  False; 
       if  iPos  <=  Length(sVal)   then 
          begin 
             sVal[iPos] := cVal; 
             Result := True; 
          end; 
    end; 

    ,  InsertChar  True     
False    .       ,  
      .   
  , & ^   
,    ;^, 
      '  '1" """ 

    procedure InsertChar(cVal: char; var sVal: string; iPos: integer); 
    begin 
       if  iPos > Length(sVal) then 
          Raise  Exception.Create('  '); 
       sVal[iPos] :=  cVal; 
    end; 

           sVal.  , 
       ,   
False.  ,    ,    
    ,    , 
 , .       
,  

    sVal[iPos] :=  cVal; 

      . 
         ,     
      Exit  RTL (, , 
     )        , 
        
.           
 ( ,     VCL,    
-f orePost,       
).  ,     State- 
  State Machine,    . 
      Raise :                                           

    Raise <Instance>; 

     Raise     , 
, ,  ,       
  ""   .   : 

    Buttonl   := TButton.Create(Self);     
 Buttonl.       : 

    Raise  Exception.Create('  '); 

       ,      .  
    ,    .    
,          
OESD^l^iBJ^SHffil^SJ3^^^    . 
 ,     ,     
  ,       . 

     5.  ,  115 


     

         Delphi    
  .       
,    EAbort     . 
        . 
  EAbort      , 
  .   . 

    var  Delphi:   string; 
    procedure  SetDelphiVariable(sValue:   string); begin 
    if Delphi =  sValue  then Raise  EAbort.Create(''); 
    if   (Length(sValue)   >  0)   and   (Length(sValue)   <  50)   then 
    Delphi   :=  sValue; end; 
    procedure TForml.Button6Click(Sender:   TObject); begin 
    SetDelphiVariable('Anders  Hejlsberg1); end; 

     Button6Click   SetDelphiVariable,   
 .      sValue, 
      Delphi.  
  ,  .   Delphi 
   ,   sValue,      
.        
Delphi  sValue.   ,    
EAbort,      -  . 
     ,    SetDelphiVariable  
Button6Clickjae ^ try.     
   ,  "Application. 
OnException,       EAbort. ,  
  SetDel-phiVajriable    try,  , 
   except  finally,  , -. , 
,        . 

        

          Exception   SysUtils.  
   .     on. . . do,  
   ,     
  .    ? ,   
   ,           , 
  public  published.   Exception   
SysUtils    5.4. 

     5.4. \UDELPHI3\CHP5\SYSUTILS. PAS.    - 
  Exception 

    Exception = class(TObject) private 
    FMessage: string; 
    FHelpContext: Integer; public 
    constructor Create(const Msg: string); 
    constructor CreateFmt(const Msg: string; const Args: array of const); 
    constructor CreateRes(Ident: Integer); 
    constructor CreateResFmt(Ident: Integer; const Args: array of const); 
    constructor CreateHelp(const Msg: string; AHelpContext: Integer); 
    constructor CreateFmtHelp(const Msg: string; const Args: array of const; 
AHelpContext: Integer); 
    constructor CreateResHelp(Ident: Integer; AHelpContext: Integer); 
    constructor CreateResFmtHelp(Ident: Integer; const Args: array of const; 


    116  I.   

    AHelpContext:   Integer); 
    property HelpContext:   Integer  read  FHelpContext write  FHelpContext; 
property Message:   string  read  FMessage  write  FMessage; end; 

     Exception-      .  
   EAbort, EAccessViolation, EInvalidPointerOperation  
 .        
 ,    .   
 ,       Messa 
    ge,   public.      
    Jiel_?Context  ^ 
_^111, 1^^ IBi _, .  ~, ..... 

      Exception 

      Exception    .   
      .   
    ,      : 
 ,    ,   
.    ,       Message.  
    ,     
    . ,      
           
Application. ShowException. 
        Create.     . 
     .   CreateFmt    
,         ; 
 ,    ,   
.  CreateRes      
.        
,     .    , 
    ,  .   
   ,     . 

        

           Exception, 
          
.   ,        
   Exception. 
            Exception. 
            . 
       Exception       
 ErrorCode ( ).  ,     
         
  ,      ,  
    ,    
    Application. OnException.  ,    
  ,     Message,   . 

    procedure  TGlobalExHandler.HandlerProc(Sender:   TObject;   EInstance:   
Exception); begin 
    if pos('EOutOfResource',   EInstance.Message)   >  0  then 
    { EOutOfResource.        .} end; 

       ,      
           
 case.   . 

    procedure TGlobalExHandler.   HandlerProc(Sender:   TObject;   EInstance:  
 Exception); begin 
    if  Sender  is  EMyComponentError  then case   (Sender  as  
TComponent).Tag  of 
    clnvalidValue:   {  .} cRangeError     :   
{  .} else begin 
    ShowMessage('  .     ') 


     5.  *  117 


    Application.Terminate; end; end; end; 

             
   EMyComponentException   .  
  ,   case. . .of.   
        ErrorCode. 
   ,       , 
    .   5.5 ,  
    EMyComponentError. 
    f"  5.5. \UDELPHI3\CHP5\PROJECT1 .-DPR.   
  Exception 

    type 
    EMyComponentError = class(Exception) private 
    FErrorCode: integer; public 
    property ErrorCode: integer read FErrorCode write FErrorCode; 
    constructor Create (const Msg: string; ErrCode: integer); end; 
    implementation 
    constructor EMyComponentError. Create(const Msg: string; ErrCode: 
integer); begin 
    Inherited Create(Msg); 
    FErrorCode := ErrCode;; end; 
    end. 

            
EMyComponentError,    Exception.    
  ErrorCode       
 FErrorCode.  ,     
Create     (    
).     ,     
  ErrorCode.      
  . 

    Raise EMyComponentError.Create( 
    '     ',   cRangeError); 

            . 
 . 

    procedure TGlobalExHandler.HandlerProc(Sender: TObject; EInstance: 
Exception); begin 
    if Sender is EMyComponentError then begin 
    if Sender is EMyCompoRangeError then 
    ( .} else if Sender is EMyCompoInvalidValue then 
    { .} end; else 
    begin 
      ShowMessage(' .  ') Application.Terminate; 
    end; 
end; 


     I.   

             
  -nentError.     
     .   
     : 

    type 

    EMyCompoRangeError = class(EMyComponentError); 
    EMyCompoInvalidValue = class(EMyComponentError); 

        EMyComponentError,  EMyCompoRangeError  
EMyCompoInvalidValue       
Exception.    EMyComponentError.      
 , ,      False,   
   EMyCompoRangeError  EMyCompoInvalidValue. 

     

             
Delphi,        
.    ,      "" 
       try. . . except  
try. . . finally.     ,  , 
    . ,   ,   
       . 

     5.    119 

