Try
Finally
Except

解决方案 »

  1.   

    try
    finally
    end
    出了异常,执行finally中的代码,不出异常也执行,这个一般用来内存释放,而且会向调用函数上报异常。try
    except
    end;
    这个是出了异常执行except中的代码,不出异常不执行,这个不会向调用函数上报异常。
      

  2.   

    必须嵌套使用。
    //分配资源
    try
      try
        //使用资源
      except
        //异常处理
        raise;
      end;
    finally
      //释放资源
    end;
      

  3.   

            try
              sys_datamodule.con1.Connected:=true;
            except
              Application.MessageBox('服務器連接配置錯誤,請重新配置!!!','提示',MB_OK+MB_ICONINFORMATION);
              sys_setup_f:=Tsys_setup_f.Create(Application);
              sys_setup_f.ShowModal;
              sys_setup_f.Free;
              sys_datamodule.Free;
              IsCancel:=True;
              Application.Run;
              Application.Terminate;
            end;
      

  4.   

        if qry_hrqj.State in [dsedit,dsinsert] then
        begin
          try
            isSave:=true;
            qry_hrqj.Post;
          finally
            ChangeStatus(sBrowse);
          end;  
        end;
      

  5.   


    正解
    except和finally不能同时使用,但可以嵌套着用,就象4楼所说的
      

  6.   

      try
     ...
    except
      on EZeroDivide do HandleZeroDivide;
      on EOverflow do HandleOverflow;
      on EMathError do HandleMathError;
    end;
    An exception handler can specify an identifier before the name of the exception class. This declares the identifier to represent the exception object during execution of the statement that follows on...do. The scope of the identifier is limited to that statement. For example,try
     ...
    except
      on E: Exception do ErrorDialog(E.Message, E.HelpContext);
    end;If the exception block specifies an else clause, the else clause handles any exceptions that aren't handled by the block's exception handlers. For example,try
     ...
    except
      on EZeroDivide do HandleZeroDivide;
      on EOverflow do HandleOverflow;
      on EMathError do HandleMathError;
    else
      HandleAllOthers;
    end;Here, the else clause handles any exception that isn't an EMathError.
    An exception block that contains no exception handlers, but instead consists only of a list of statements, handles all exceptions. For example,try
     ...
    except
      HandleException;
    end;Here, the HandleException routine handles any exception that occurs as a result of executing the statements between try and except.
      

  7.   

    procedure TForm1.Button1Click(Sender: TObject);
    var
      MyStringList :TStringList;
    begin
      try
        MyStringList := TStringList.Create;
        MyStringList.Add('Test1');
    //.......
      finally  //保證在正常或出錯的情況下都能釋放"MyStringList".
        MyStringList.Free;
        MyStringList := nil;
      end;
    end;
      

  8.   

    Reset(F);try
     ...  // process file F
    finally
      CloseFile(F);
    end;The syntax of a try...finally statement istry statementList1 finally statementList2 endwhere each statementList is a sequence of statements delimited by semicolons. The try...finally statement executes the statements in statementList1 (the try clause). If statementList1 finishes without raising exceptions, statementList2 (the finally clause) is executed. If an exception is raised during execution of statementList1, control is transferred to statementList2; once statementList2 finishes executing, the exception is re-raised. If a call to the Exit, Break, or Continue procedure causes control to leave statementList1, statementList2 is automatically executed. Thus the finally clause is always executed, regardless of how the try clause terminates.If an exception is raised but not handled in the finally clause, that exception is propagated out of the try...finally statement, and any exception already raised in the try clause is lost. The finally clause should therefore handle all locally raised exceptions, so as not to disturb propagation of other exceptions.
      

  9.   

    怎么我看到头胀胀的 >.<ll
      

  10.   

    下面"傻子1"都執行,"傻子2"不執行,請細味區別try
    我是救世主 
    except
    傻子1
    end;try
    我是救世主 
    finally
    傻子1
    end;try
    我不是救世主 
    except
    傻子2
    end;try
    我不是救世主 
    finally
    傻子1
    end;