Whilst we all want to spend our time writing functional code, errors will and do occur in in code from time to time. Sometimes, these are outside of our control, such as a low memory situation on your PC.
In serious code you should handle error situations so that at the very least, the user is informed about the error in your chosen way.
Delphi uses the event handling approach to error handling. Errors are (mostly) treated as exceptions, which cause program operation to suspend and jump to the nearest exception handler. If you don't have one, this will be the Delphi default handler - it will report the error and terminate your program.
Often, you will want to handle the error, and continue with your program. For example, you may be trying to display a picture on a page, but cannot find it. So you might display a placeholder instead. Much like Internet Explorer does.
又來蠱惑人心。你用都沒用過怎么知道它沒用?
操作1
except
操作2
end
当操作1因为系统以外的某些原因失败时,执行操作2,防止死机或者系统执行错误操作
所以在一些容易出错的地方(如:数据库操作),最好使用该语句
intValue: Integer;
begin
try
intValue := StrToInt('131aaa');
except
// 若没有 try ... except 来捕获异常, 则程序会出现一个错误对话框,
// 而用了异常捕获则不会出现错误对话框, 这样会更加友好.
end;
end;
try
adoconn.open;
except
showmessage('not login,please set up the conn');
end;
要做的事件 要做的事件
finally except
要做的事件正常完成后做的事件 要做的事件正没常完成后做的事件
end; end;
begin
try
//此处是插入记录的代码
except
Application.messagebox('记录重复!','提示',MB_OK+MB_IconInformation);
end;
end;
不知道你不用try语句的时候,这样的情形是如何处理的呢?
打个很简单的比方,你要插入一条记录,但该数据表设了唯一性的主键,那么你在插入记录的时候,就可以把insert语句写在try中,如果你不用try的话,你想程序不出错,那就得在插入记录之前自己先写一些代码来判断一下这条记录能否插入了。with adoqurty1 do
begin
try
//此处是插入记录的代码
except
Application.messagebox('记录重复!','提示',MB_OK+MB_IconInformation);
end;
end;
不知道你不用try语句的时候,这样的情形是如何处理的呢?
我已经考虑进去了,会先检测,所以,不可能有重复的记录,而且有提示,
再用try...except...end
不是多余了吗?
如读取磁盘中的数据, 当你读到一半时操作系统检测到当前簇有问题, 抛出一个异常来, 你若不加 try ... except 捕获, 可能会产生内存来不及释放, 等等可能.只要有与硬件交互你就不可能完全避免异常, 打个比方: 在人行道上走路, 你觉得只要自己注意点就不会摔跤, 突然路塌陷下去, 你根本来不及反应就掉进水坑里.
cutedelphigirl (delphi女孩) 如果你是MM的话,留个QQ,俺细细教你。哈哈
你把每个可能出错的地方都事先写好代码,确实是能避免程序出错,可是你不觉得用了try更方便吗?代码也会少很多。
编程
except
睡觉
finally
编程
end;
((这年头什么人都敢乱放精通delphi的,
具体内容如下:
要更新两个表,这两个表的数据必须同时更新,后者都不更新当更新了table1时,突然网络中断,而数据库不在本地,如何判断?
你能保存你的程序是时时刻刻能连接库的吗, 万一连接不上,而执行query.open ,你想想会出现什么样的状况?
理论我就不用了,前面的兄弟说得很明白了,在这我只是举个例子说明重要性
try
.......
except
on Exception do
ShowMessage('程序出错了!不过别问我为什么:-)');
end
编译成。EXE文件就可以看得比较清楚了!
tryexceptend
比如,你要打开一个文件,它有可能不存在的啊
那你就
try
reset(f);
except
showmessage('文件打开出错!');
finally
//做一些处理
end;
reset(f);
except
showmessage('文件打开出错!');
finally
//做一些处理
end;delphi一直到7都不支持这种结构。
必须用镶套
In serious code you should handle error situations so that at the very least, the user is informed about the error in your chosen way.
Delphi uses the event handling approach to error handling. Errors are (mostly) treated as exceptions, which cause program operation to suspend and jump to the nearest exception handler. If you don't have one, this will be the Delphi default handler - it will report the error and terminate your program.
Often, you will want to handle the error, and continue with your program. For example, you may be trying to display a picture on a page, but cannot find it. So you might display a placeholder instead. Much like Internet Explorer does.