如何拦截错误信息????(200分)

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

Fins

Unregistered / Unconfirmed
GUEST, unregistred user!
我在设计程序时如何出现一些意外的错误,我又不想叫DELPHI自己拦截这些信息,我如何
获得这些错误的信息代码???
 
try
except
end;
 
这样只能拦截指定的错误,如果不知识错误代码就不能够进行处理。
 
Exceptions are handled within try...except statements. For example,
try
X := Y/Z;
except
on EZeroDivide do HandleZeroDivide;
end;
This statement attempts to divide Y by Z, but calls a routine named HandleZeroDivide if an EZeroDivide exception is raised.
The syntax of a try...except statement is
try statements except exceptionBlock end
where statements is a sequence of statements (delimited by semicolons) and exceptionBlock is either
another sequence of statements or a sequence of exception handlers, optionally followed by
else statements
An exception handler has the form
on identifier: type do statement
where identifier: is optional (if included, identifier can be any valid identifier), type is a type used to represent exceptions, and statement is any statement.
A try...except statement executes the statements in the initial statements list. If no exceptions are raised, the exception block (
exceptionBlock) is ignored and control passes to the next part of the program.
If an exception is raised during execution of the initial statements list, either by a raise statement in the statements list or by a procedure or function called from the statements list, an attempt is made to 揾andle?the exception:
If any of the handlers in the exception block matches the exception, control passes to the first such handler. An exception handler 搈atches?an exception just in case the type in the handler is the class of the exception or an ancestor of that class.
If no such handler is found, control passes to the statement in the else clause, if there is one.
If the exception block is just a sequence of statements without any exception handlers, control passes to the first statement in the list.
If none of the conditions above is satisfied, the search continues in the exception block of the next-most-recently entered try...except statement that has not yet exited. If no appropriate handler, else clause, or statement list is found there, the search propagates to the next-most-recently entered try...except statement, and so forth. If the outermost try...except statement is reached and the exception is still not handled, the program terminates.
When an exception is handled, the stack is traced back to the procedure or function containing the try...except statement where the handling occurs, and control is transferred to the executed exception handler, else clause, or statement list. This process discards all procedure and function calls that occurred after entering the try...except statement where the exception is handled. The exception object is then automatically destroyed through a call to its Destroy destructor and control is passed to the statement following the try...except statement. (If a call to the Exit, Break, or Continue standard procedure causes control to leave the exception handler, the exception object is still automatically destroyed.)
In the example below, the first exception handler handles division-by-zero exceptions, the second one handles overflow exceptions, and the final one handles all other math exceptions. EMathError appears last in the exception block because it is the ancestor of the other two exception classes; if it appeared first, the other two handlers would never be invoked.
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抰 handled by the block抯 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抰 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
 
我一直都用这种方法来记录错误信息,它将在程序当前目录中生成一LOG文件,记录错误信息。
先在程序里放一个ApplicationEvents组件,然后在它的OnException事件
中编程(我这段代码是针对MDI程序的)
procedure TMainForm.ApplicationEvents1Exception(Sender: TObject;
E: Exception);
var
FileName:string;
ErrLog:TextFile;
CFormName,CTRLName:string;
begin
CFormName:=ActiveMDIChild.Caption ;//取得子窗体的名字
CTRLName:=ActiveMDIChild.ActiveControl.Name ;//取当前操作控件的名字
FileName:=ChangeFileExt('Error','.log');//创建一个Error.log文件
AssignFile(ErrLog,FileName);
if FileExists(FileName) then
Append(ErrLog)
Else
ReWrite(ErrLog);
Writeln(ErrLog,DateTimeToStr(Now)+':'+ CFormName +'/'+CTRLName+'/'+E.message);//写入错误信息
Application.ShowException(E);
CloseFile(ErrLog);
end;
 
楼上说的对,一般我是在程序里放一个ApplicationEvents组件,然后在它的OnException事件
中编程就可以了,当然,最好的办法是装上CodeSite,让CodeSite来捕获你的出错事件,像
这样:Application.OnException := CodeSite.ExceptionHandler,好了,一切都搞定了!
 
多人接受答案了。
 
后退
顶部