Basic principles and methods for exception handling in Delphi

Source: Internet
Author: User

Basic principles and methods for exception handling in Delphi


1. Source of the exception.

In Delphi applications, exceptions may occur in the following cases.
(1) File Processing
(2) Memory Allocation
(3) Windows Resources
(4) create objects and forms at run time
(5) Hardware conflicts with the operating system

Ii. Handling exceptions.

(1) Try... Else T... End;
When the code in the Try body encounters an exception, the system will switch to the fail t part for exception handling. This is one of the most basic ways for Delphi to handle 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 is abnormal, all windows objects must be correctly processed by the system.

And try... Else T... If end is different, the finally part of the structure is always executed.


(3) Try… does not exist... Else T... Finally... The end structure not only handles exceptions, but also protects the structure of resource allocation. However, try... Else T... The end structure allows nesting to try... Finally... In the end structure to ensure both Exception Handling and resource allocation.


Iii. precise handling of exceptions.

(1) define an exception.
In Delphi, each exception is a derived class of exception [1] [2]. Therefore, defining an exception is to define a derived class of the exception class.
Type emyexception = Class (exception );
Of course, the base class can be a derived class at any level of exception or exception.


(2) throw an exception in the program.
Throwing an exception based on different situations is the most basic mode of exception. In Delphi, it is implemented by the raise statement.
[Syntax] Raise exception class. Create ('default exception description ');


(3) try... Else T... More precise exception capturing in end.
Use on E: exception class do... Structure can handle exceptions thrown by specific exception classes in the do body.


4. Exception debugging.

In Delphi IDE, remove "Debugger options" (you can use menu tools> debugger OPTIONS... Check the status of the integrated debugging check box to debug exceptions.


5. Supplementary description of exceptions.

(1) errors may occur in each program! This is an unquestionable phenomenon and law in the software industry. In fact, the traditional if... Else... The structure can completely solve all the errors, and the exception mechanism cannot be used to avoid exceptions at the most primitive level by traversing possible situations. So, why do we need an exception mechanism?

The answer is clear: Exceptions provide a more flexible and open way, so that later programmers can handle such errors according to the actual situation, rather than using preset handling results. In fact, I think this is the core of the exception mechanism.


--------------------------------------------------------------------------------

[1] The exception class is defined in the sysutils unit.
[2] Delphi also supports exception classes that do not inherit from exceptions, but I think it is not wise to do so.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.