Each statement in the except part of a try..except block defines code to execute to handle a particular kind of exception. The form of these exception-handling statements is as follows:
on do ;
Example
You can define an exception handler for division by zero to provide a default result:
function GetAverage(Sum, NumberOfItems: Integer): Integer;
begin
try
Result := Sum div NumberOfItems; { handle the normal case }
except
on EDivByZero do Result := 0; { handle the exception only if needed }
end;
end;
Note that this is clearer than having to test for zero every time you call the function. Here's an equivalent function that doesn't take advantage of exceptions:
function GetAverage(Sum, NumberOfItems: Integer): Integer;
begin
if NumberOfItems <> 0 then { always test }
Result := Sum div NumberOfItems { use normal calculation }
else Result := 0; { handle exceptional case }
end;
The difference between these two functions really defines the difference between programming with exceptions and programming without them. This example is quite simple, but you can imagine a more complex calculation involving hundreds of steps, any one of which could fail if one of dozens of inputs were invalid.
By using exceptions, you can spell out the "normal" expression of your algorithm, then provide for those exceptional cases when it doesn't apply. Without exceptions, you have to test every single time to make sure you're allowed to proceed with each step in the calculation.
>
> 비베에서는 On Error goto문이 있습니다만
> 델파이에서는 어떤식으로 구현하는지 알고 싶습니다.
>
> 예를들어 파일을 읽어들일때 파일이 없다거나,
> 형이 맞지 않아 에라가 발생할때는 에라문을
> 실행하도록 하고 싶은데요.
Each statement in the except part of a try..except block defines code to execute to handle a particular kind of exception. The form of these exception-handling statements is as follows:
on
Example
You can define an exception handler for division by zero to provide a default result:
function GetAverage(Sum, NumberOfItems: Integer): Integer;
begin
try
Result := Sum div NumberOfItems; { handle the normal case }
except
on EDivByZero do Result := 0; { handle the exception only if needed }
end;
end;
Note that this is clearer than having to test for zero every time you call the function. Here's an equivalent function that doesn't take advantage of exceptions:
function GetAverage(Sum, NumberOfItems: Integer): Integer;
begin
if NumberOfItems <> 0 then { always test }
Result := Sum div NumberOfItems { use normal calculation }
else Result := 0; { handle exceptional case }
end;
The difference between these two functions really defines the difference between programming with exceptions and programming without them. This example is quite simple, but you can imagine a more complex calculation involving hundreds of steps, any one of which could fail if one of dozens of inputs were invalid.
By using exceptions, you can spell out the "normal" expression of your algorithm, then provide for those exceptional cases when it doesn't apply. Without exceptions, you have to test every single time to make sure you're allowed to proceed with each step in the calculation.