Delphi Master Breakthrough exception and error handling

Source: Internet
Author: User

What is an exception? Why do you use it?

The so-called "exception" refers to an object of an exception class. In Delphi's VCL, all exception classes are derived from the exception class. This class declares the general behavior and nature of the exception. Most importantly, it has a message property that can report the reason for the exception to occur.

It should be emphasized, however, that exceptions are used to flag errors, but not to cause errors to occur. The exception is caused only by encountering raise, and at any time, even if no error occurs, raise will cause an exception to occur. Exceptions occur only because of raise, not others! By throwing an exception to handle an unexpected situation, you can guarantee that all code in the program's main process is available without having to add a complex judgment statement. For example, function a throws an exception:

[Delphi]View PlainCopy
  1. function A (): Integer;
  2. Vat
  3. Pfile:textfile;
  4. Begin
  5. ...... //Some code
  6. PFile: = Somefunctiontoopenanfile ();
  7. If PFile = nil Then
  8. Raise Exception. Create (′open file failed!′); //File open failed throwing exception
  9. Read (PFile, ...); //Read file
  10. ...... //Some other actions on the file, at which point the file pointer is guaranteed to be valid
  11. End

The code for function A makes it easy to handle file open errors. If the file fails to open, a exception class exception object is thrown, and the function returns immediately, thereby protecting the following operation against the file pointer from being executed. The thrown exception also needs to be captured and processed. Assuming that function B calls function A, to catch this file opening the failed exception, you need to preset a trap before calling a, which is called the "Try...except block". First look at the code for function B:

[Delphi]View PlainCopy
    1. Procedure B ();
    2. Begin
    3. ...... //Some code
    4. Try
    5. A (); //Call a
    6. Somefunctiondependona (); //function dependent on the result of a
    7. Except
    8. ShowMessage (′some error occured′); //Hey, I fell in, there was an anomaly .
    9. End;
    10. ...... //Continuation of the Code
    11. End


A throws an exception that will be captured by the try...except set by B. Once the exception is caught, the subsequent sensitive code is no longer executed, but instead immediately jumps to the except block to perform error handling before proceeding with the execution of the code after the entire try block. Control over the program flow is left in function B.

http://blog.csdn.net/sushengmiyan/article/details/7506421

Delphi Master Breakthrough exception and error handling

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.