讨论异常处理(你未必就真的能讲的清楚)(100分)

  • 主题发起人 fanwendou
  • 开始时间
F

fanwendou

Unregistered / Unconfirmed
GUEST, unregistred user!
对异常处理请问各位大虾,有什么高的见解.
下面的一段异常处理我还有不解处请指教
implementation
type
ME =class(Exception)
//写入错误信息到文件
procedure WriteErrtoFile(const FileName:string);
end;
{$R *.dfm}
//实现写入文件操作
procedure me.WriteErrtoFile(const FileName:string);
var
f:TextFile;
begin
AssignFile(f,FileName) ;
Append(f);
Writeln(f,message);
CloseFile(f);
end;

procedure someoperation;
var
x,y:Integer;
myErr:ME;
begin
x:=1;
y:=0;
if x>y then
begin
myErr:=me.create('发生ME类型的错'+#13#10+'错误的位置在TForm1.someoperation'
+'已经写入日志') ;
myErr.WriteErrtoFile('E:/Err.txt');
raise myErr;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
try
someoperation;
except
on E:medo
begin
MessageDlg(e.Message,mtError,[mbOK],0);
end;
end;
end;

在"on E:medo
"这个地方怎么理解?
 
简单点说就是捕获异常,而且将异常转化为me这种类型的异常,并用实例化的对象E来让程序员处理。
To read specific information about an exception instance in an exception handler, you use a special variation of on..do that gives you access to the exception instance. The special form requires that you provide a temporary variable to hold the instance. For example:
on E: EIntegerRangedo
ShowMessage(Format('Expected value between %d and %d', E.Min, E.Max));
The temporary variable (E in this example) is of the type specified after the colon (EIntegerRange in this example). You can use the as operator to typecast the exception into a more specific type if needed.
 
to TYZhang
非常的详细,谢谢!还想请教有关异常处理其它的高见,很想了解,谢谢!
请各位高手不吝赐教.
 
我给你转个帖子,你看看:
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1801855
你也可以在坛子里搜索一下,关于异常的帖子很多。
异常的处理其实很简单,只是delphi的异常机制设计的比较巧妙,没有太多的人去深入研究而已。
 
其实就是 seh
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
554
import
I
I
回复
0
查看
617
import
I
顶部