try finally 块 是做什么的?

解决方案 »

  1.   

    a:=Ta.Create;//创建对象
    try
      a.DoSomething;//调用对象a的方法,这个方法可能会触发某些异常
    finally      //确保在产生异常后,下面的语句能执行——比如释放对象
      a.Free;
    end;  
      

  2.   

    出错处理
    我程序中的一段,自己看看
    try
    hlp.BorderStyle := bsNone;
    hlp.SetBounds(0, 0, 1, 1);
    hlp.FormStyle := fsStayOnTop;
    hlp.Show;
    mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
    mouse_event(MOUSEEVENTF_ABSOLUTE or MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    SetForegroundWindow(hwnd);
    finally
    hlp.Free;
    end;
      

  3.   

    try ...finally目的是:有时候为了确保会执行某段命令
    比如try 代码A finally 代码b
    其中代码B是必须执行的,不管代码A是否能够结束,就是说如果代码A没法结束的时候那么就强制执行代码b。
      

  4.   

    try finally end
    是保证发生任何异常都有被执行的一段代码,该段代码放到finally end之间
      

  5.   

    靠,csdn搞什么,我给3个人分不让我结帖,说什么回复次数大于给分次数,那我还给自己几分?
      

  6.   

    上面说的都是很实用的情况,但如果你要了解的全面,建议你有什么问题都看delphi的帮助:where 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.从第一段:关键在最后一句Thus the finally clause is always executed, regardless of how the try clause terminates.无论如何finally中的语句一定被执行,所以我们编程的时候要养成习惯,将一些必须要执行的(如第一位所说的,释放资源等)放在finally块里。从第二段:例举了一特殊情况,an exception is raised but not handled in the finally clause这时,在它后面发生的exception没有机会raise(在try块中raise),所以它建议在finally块中处理所以的raise。
      

  7.   

    楼主~~csdn之所以不让你结贴,因为偶还在给你写贴啊!!
      

  8.   

    try异常处理,finally end之间为不管结果如何最终都要执行的代码
    接分了
      

  9.   

    var
      ADQTry: TADOQuery;
    begin
      ADQTry:=TADOQuery.Create(Self);
      try
        ADQTry.Connection:=MainData.AdcConn;
        ADQTry.SQL.Add('SELECT * FROM SSSS');
        ADQTry.Open
      finally
        ADQTry.Close;
        ADQTry.Free;
      end;
    end;//不论是否出错,动态产生的ADQTry保证从内存中释放