Try
Finally
Except
Finally
Except
解决方案 »
- 记录显示的问题请教
- 请问 Wallpaper Calendar 是如何实现在桌面显示日历和输入数据的
- 郁闷!请各位帮忙啊!
- 关于PDF文件
- 基于TCustomControl之上开发的组件如何获得焦点。谢谢。
- 用QuickReport做的报表,挺简单的。但一点打印或打印机设置,就死机
- 在使用 ExpressBars 控件包的问题
- 我用Query写insert into 语句到oracle数据库。。怎么提交?这样写可以吗?
- 关于listbox,memo的问题。我等待回答
- DELPHI中怎样用SQL语句更新数据库中数据?要注意什么?有例子的话高分。。。。我会加的
- EfileError in module rtl60.bpl.A class named TraCodeModule already exists.
- 如何让delphi7开发主窗口不透明,现在可以看到delphi窗口后面的界面,不习惯
finally
end
出了异常,执行finally中的代码,不出异常也执行,这个一般用来内存释放,而且会向调用函数上报异常。try
except
end;
这个是出了异常执行except中的代码,不出异常不执行,这个不会向调用函数上报异常。
//分配资源
try
try
//使用资源
except
//异常处理
raise;
end;
finally
//释放资源
end;
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;
begin
try
isSave:=true;
qry_hrqj.Post;
finally
ChangeStatus(sBrowse);
end;
end;
正解
except和finally不能同时使用,但可以嵌套着用,就象4楼所说的
...
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.
var
MyStringList :TStringList;
begin
try
MyStringList := TStringList.Create;
MyStringList.Add('Test1');
//.......
finally //保證在正常或出錯的情況下都能釋放"MyStringList".
MyStringList.Free;
MyStringList := nil;
end;
end;
... // 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.
我是救世主
except
傻子1
end;try
我是救世主
finally
傻子1
end;try
我不是救世主
except
傻子2
end;try
我不是救世主
finally
傻子1
end;