请问:Raise 是干吗的?

zing21cn 2003-12-05 08:37:40
请问:Raise 是干吗的?
...全文
172 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
codehunter008 2003-12-05
  • 打赏
  • 举报
回复
就是触发一个异常,你可以自己来做!
hejianling305 2003-12-05
  • 打赏
  • 举报
回复
To raise an exception object, use an instance of the exception class with a raise statement. For example,

raise EMathError.Create;

In general, the form of a raise statement is

raise object at address

where object and at address are both optional; see Re-raising exceptions. When an address is specified, it can be any expression that evaluates to a pointer type, but is usually a pointer to a procedure or function. For example:

raise Exception.Create('Missing parameter') at @MyFunction;

Use this option to raise the exception from an earlier point in the stack than the one where the error actually occurred.

When an exception is raised--that is, referenced in a raise statement--it is governed by special exception-handling logic. A raise statement never returns control in the normal way. Instead, it transfers control to the innermost exception handler that can handle exceptions of the given class. (The innermost handler is the one whose try...except block was most recently entered but has not yet exited.)

For example, the function below converts a string to an integer, raising an ERangeError exception if the resulting value is outside a specified range.

function StrToIntRange(const S: string; Min, Max: Longint): Longint;
begin
Result := StrToInt(S); // StrToInt is declared in SysUtils
if (Result < Min) or (Result > Max) then
raise ERangeError.CreateFmt(
'%d is not within the valid range of %d..%d',
[Result, Min, Max]);
end;

Notice the CreateFmt method called in the raise statement. Exception and its descendants have special constructors that provide alternative ways to create exception messages and context IDs.

A raised exception is destroyed automatically after it is handled. Never attempt to destroy a raised exception manually.

Note

Raising an exception in the initialization section of a unit may not produce the intended result. Normal exception support comes from the SysUtils unit, which must be initialized before such support is available. If an exception occurs during initialization, all initialized units--including SysUtils--are finalized and the exception is re-raised. Then the exception is caught and handled, usually by interrupting the program. Similarly, raising an exception in the finalization section of a unit may not lead to the intended result if SysUtils has already been finalized when the exception has been raised.
xiaocuo_zrf 2003-12-05
  • 打赏
  • 举报
回复
用来把错误扩散的
比如
定义:
function Test:Boolean
begin
try
..你函数的处理
except
raise //
end;
end;
调用
BEGIN
try
IF Test THEN
begin
...
end;
excepet
Showmessage('Error');//如果上面函数没有RAISE此处的异常就不会被引发
end;
END;
qgj99 2003-12-05
  • 打赏
  • 举报
回复
是用来抛出一个异常,经常用在
try
...
except
...
raise
....

5,386

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧