First, the source of abnormalities.
In Delphi's application, the following conditions are more likely to produce exceptions.
(1) File processing
(2) Memory allocation
(3) Windows resources
(4) Creating objects and forms at runtime
(5) Hardware and operating system conflicts
?
Second, abnormal treatment.
(1) Try ... Except ... End;
When the code in the try is abnormal, the system will turn to the Except section for exception processing. This is one of the most basic ways of Delphi processing exceptions.
?
(2) TRY ... FINALLY ... END;
This abnormal processing structure is generally used to protect Windows resource allocation, etc., it ensures that the correct processing of some Windows objects that makes the final unified processing by the system regardless of whether the code in the try body is required.
Unlike Try ... Except ... End, the Finally section of this structure is always performed.
?
(3) There is no Try ... Except ... Finally ... END structure to handle exception, but also protect resource allocation, but Try ... Except ... End structure allows nested to the Try ... Finally ... END structure, thereby improving both And protect the allocation of resources.
?
?
Third, the precise handling of exceptions.
(1) Define an exception.
In Delphi, each exception is a derived class of the Exception [1] class [2]. Therefore, defining an exception is a derived class that defines an Exception class.
TYPE EMYEXCEPTION = Class (Exception);
Of course, the base class can be an exception or any level of derived classes of any level.
?
(2) Throw an exception in the program.
Throwing anomalies according to different situations are the most basic modes of using anomalies. In Delphi, it is implemented by the RAISE statement.
[Syntax] Raise anomaly .create ('Abnormal Default Description ");
?
(3) More precise capture exceptions in Try ... ExcePt ... End.
Using the ON E: Abnormal class DO ... structure can handle the exception thrown in the DO body.
?
Fourth, abnormal debugging.
In Delphi IDE, the hook status of the Integrated Debugging check box in "Debugger Options" can be accessed using menu Tools-> Debugger Options ... can be debugged.
?
Five, an abnormal supplement.
(1) Each program may have an error! This is an unquestionable phenomenon and law in the software industry. In fact, the traditional if ... Else ... structure can solve all errors, using the Exception mechanism can not be avoided in the most original hierarchy, through traversal possible cases, the abnormal approach, then why do you have an abnormal mechanism?
The answer is clear: An abnormality provides a more flexible and open mode such that the later programmers can process this error according to the actual situation, rather than using pre-set processing results. In fact, I think this is the core of the abnormal mechanism.
[1] The Exception class is defined in the SYSUTILS unit.
[2] Delphi also supports unusual classes that are not inherited from Exception, but I think it is not very wise.