"Delphi master breaks through"

zhaozj2021-02-08  239

Completed the third chapter of abnormal and error handling, an excerpt

Constructor and abnormal

This topic is often mentioned in the C community, and there seems to be never been taken in the Delphi community. Perhaps due to the characteristics of the language, the Delphi programmer does not have to care about this problem. But I think the Delphi programmer should also know about the problem. I know what the language provides us with so that we are so easy and don't pay attention to it. It is the so-called "Body to know the blessing".

We know that the constructor of the class is not returned. If the constructor construct object fails, it is impossible to rely on the error code. So how do I identify the failure of the constructor in the program? The most "standard" method is to throw an exception.

The constructor failed, which means that the structure of the object fails, then throws an exception, what is the "half-dead" object will handle?

Here, I want to read it if necessary to have a way of handling this situation in this case.

In C , after the constructor throws an abnormality, the destructive function will not be called. This approach is reasonable because the object is not constructed.

If the constructor has made some operations such as allocating memory, open files, then the C class needs to have their own members to remember what actions have been done. Of course, this is very troublesome for the implementation of the class, so the implementation of the general C class avoids throwing exceptions in the constructor (which can provide a member function such as init and uninit, the constructor or class customers Call them to handle initialization failures). The scheme provided by each C classic work is to use a smart pointer (STL standard auto_ptr).

In Object Pascal, this problem becomes very simple, and programmers do not have to pay for it. If the class of Object Pascal throws an exception in the constructor, the compiler will automatically call the class's destructor (since the destructor is not allowed to be overloaded, it can guarantee that only the only destructor is only, so the compiler will not be confused Multiple destructuring functions). The destructor is generally decentralized in the sectors, and the free () method guarantees that the destructor does not call the NIL object (the member object that has not been created), so it is guaranteed by making the code simple and beautiful. Safety.

TYPE myclass = Class

Private

FSTR: PCHAR; // String pointer

public

Constructor crete ();

DEStructor destroy (); OVERRIDE;

END;

Constructor myclass.create ();

Begin

FSTR: = Stralloc (10); // Distribute memory for string pointer in the constructor

Strcopy (fstr, 'abcdefghi);

Raise Exception.create ('Error'); // Throw an exception, there is no reason, huh, huh

END;

DEStructor a.destroy ();

Begin

STRDISPOSE (FSTR); // Release memory in the sector function

Writeln ('Free Resource');

END;

VAR

Obj: TMYCLASS;

i: integer;

Begin

Try

Obj: = tmyclass.create ();

Obj.free ();

Writeln ('succeeded');

Except

Obj: = NIL;

Writeln ('failed');

END;

READ (I); // Pause the screen to observe the resulting result END.

In this code, the constructor throws an exception, the result of the execution is:

Free resource

Failed

The "Free Resource" output at this time is generated by the compiler automatically calls the destructor.

Therefore, if the class of the description document or class of the class tells you, the constructor of the class may throw an exception, then remember to use Try ... Except to package it!

C

versus

Object Pascal

Different processing methods after the constructor throw an abnormally, is a reflection of the design ideas of the two languages.

C

Adhering

C

The style, payment of efficiency, everything is handed over to the programmer, the compiler does not do extra action.

Object Pascal

inherit

Pascal

The style, pay attention to the aesthetic meaning of the program, the compiler helps programmers complete complex work.

Related Resources: Matlab Writing TopSIS Review Data Source Code

转载请注明原文地址:https://www.9cbs.com/read-2080.html

New Post(0)