1. The source of the abnormality.
In Delphi's application, the following situations are more likely to generate exceptions.
(1) File processing
(2) Memory allocation
(3) Windows resources
(4) Create objects and forms during runtime
(5) Hardware and operating system conflict
?
2. Exception handling.
(1) try … except … end ;
When an exception occurs in the code in the try body, the system will turn to the except part for exception processing. This is one of the most basic ways Delphi handles exceptions.
?
(2) try … finally … end ;
This exception handling structure is generally used to protect Windows resource allocation and other aspects. It ensures that no matter whether the code in the try body has an exception, the system needs to handle the correct processing of some Windows objects that are finally uniformly processed.
Unlike try...except...end, the finally part of the structure is always executed.
?
(3) There is no try...except...finally...end structure to handle exceptions and protect resource allocation structures. However, the try...except...end structure allows nesting into the try...finally...end structure, thereby realizing the handling of exceptions. , and protect the allocation of resources.
?
?
3. Accurate handling of exceptions.
(1) Define an exception.
In Delphi, each exception is a derived class [2] of the Exception[1] class. Therefore, defining an exception is defining a derived class of the Exception class.
type EMyException = class(Exception);
Of course, the base class can be an Exception or any level of Exception.
?
(2) Throw an exception in the program.
Throwing exceptions according to different situations is the most basic mode of using exceptions. In Delphi, it is implemented by the raise statement.
[Syntax] raise exception class.Create('Default description of exception');
?
(3) Catch exceptions more accurately in try...except...end.
Use the on E:Exception class do ... structure to handle exceptions thrown by a specific exception class in the do body.
?
4. Exception debugging.
In the Delphi IDE, uncheck the check status of the Integrated Debugging check box in "Debugger Options" (you can use the menu Tools—>Debugger Options… to access it).
?
5. Supplementary explanation of abnormalities.
(1) Every program may produce errors! This is an unquestionable phenomenon and law in the software industry. In fact, the traditional if...else... structure can completely solve all errors. Using the Exception mechanism cannot avoid the practice of generating exceptions by traversing possible situations. So, why do you still need an exception mechanism?
The answer is clear: Exceptions provide a more flexible and open way, allowing later programmers to deal with this error based on actual conditions rather than using pre-set processing results. In fact, I think this is the core of the exception mechanism.
[1] The definition of the Exception class is in the SysUtils unit.
[2] Delphi also supports exception classes that do not inherit from Exception, but I don't think it's very wise to do so.