12th Chapter-Exception Handling and program debugging (II.) (3)

Source: Internet
Author: User
Tags constructor error code exception handling reserved string to number

12.3.5.1 defines an exception object class

Exceptions are objects, so defining a new class of exceptions is not much different from defining a new object type. Because the default exception handling handles only objects inherited from exception or exception subclasses, custom exception classes should be subclasses of exception or other standard exception classes. Thus, if a newly defined exception is thrown in a module that does not contain the corresponding exception response, the default exception handling mechanism will respond with an exception, displaying a message box containing the exception class name and error message.

The following is the definition of an exception class:

Type

Emyexception = Class (Exception);

12.3.5.2 Self-throwing exception

Throws an exception, calls the reserved word raise, followed by an instance of an exception class.

If the definition:

Type

Epasswordinvalid = Class (Exception);

The following statement in your program throws a Epasswordinvalid exception:

If Password <> Correctpassword Then

Raise Epasswordinvalid.create (' incorrect Password entered ');

When an exception arises, the value of the variable erroraddr defined in the System Library unit is placed as the address where the application produces an exception. You can refer to the value of ERRORADDR in your exception handling process.

When you throw an exception yourself, you can also assign a value to the erroraddr.

Assigning an error address to an exception requires the use of a reserved word at, using the following format:

Raise Einstance at Address_expession;

Examples of application of 12.3.5.3 custom exceptions

Here's a complete example that uses custom exception programming.

The two label boxes (Label1, Label2) indicate the function of the corresponding edit box. Edit boxes Password and inputedit are used to enter passwords and numbers. Label2, Inputedit are not visible when the program starts. When the correct password is entered in the password, Label2 and InputBox appear on the screen. At this time Label1, password hidden.

At design time, the Visible property of Label2, Inputedit is false. By setting the password PasswordChar, you can determine the characters that echo back on the screen when you enter a password.

Custom exceptions Einvalidpassword and einvalidinput are used to indicate that the password entered is illegal and the number is illegal. They are subclasses of custom exception einvalidation. Instead, einvalidation derives directly from the exception exception class.

The following is the definition of three exception classes.

Type

Einvalidation = Class (Exception)

Public

Errorcode:integer;

Constructor Create (Const msg:string; Errornum:integer);

End

Einvalidpassword = Class (Einvalidation)

Public

Constructor Create;

End

Einvalidinput = Class (Einvalidation)

Public

Constructor Create (Errornum:integer);

End

Einvalidation added a public member ErrorCode to save the error code. The increase in error codes provides a great programming flexibility. For exception classes, you can provide different error messages based on error codes, and for consumers to handle exceptions outside the Try...except module by intercepting the error code.

As you can see from the above definitions, there are no arguments in the constructor parameters table for Einvalidpassword and einvalidinput that indicate an error message. In fact, they are stored inside the constructor. The following is the implementation code for the three custom exception class constructors.

Constructor Einvalidation.create (Const msg:string; Errornum:integer);

Begin

Inherited Create (MSG);

ErrorCode: = ErrorNum;

End

Constructor Einvalidpassword.create;

Begin

Inherited Create (' Invalid Password entered ', 0);

End

Constructor Einvalidinput.create (Errornum:integer);

Var

msg:string;

Begin

Case ErrorNum of

1:

MSG: = ' Can not convert String to number ';

2:

MSG: = ' number is out of Range ';

Else

MSG: = ' Input is Invalid ';

End

Inherited Create (Msg,errornum);

End

For einvalidinput,errorcode=1, the input is not a pure numeric sequence, and errorcode=2 indicates that the input value is out of bounds.

The password check starts when the user enters the password in the password and presses the ENTER key. The implementation code is in the password onkeypress Event process:

Procedure tform1.passwordkeypress (Sender:tobject; var key:char);

Const

CurrentPassword = ' Delphi ';

Begin

If Key = #13 Then

Begin

Try

If Password.text <> CurrentPassword Then

Raise Einvalidpassword.create;

Label2.visible: = True;

Inputedit.visible: = True;

Inputedit.setfocus;

Password.visible: = False;

Label1.visible: = False;

Except

On Einvalidpassword do

Begin

Password.text: = ';

Raise

End

End

Key:= #0;

End

End

In the same way, the legality check of the input number is realized in the Inputedit onkrypress event Processing:

Procedure tform1.inputeditkeypress (Sender:tobject; var key:char);

Var

Res:real;

Code:integer;

Begin

If Key = #13 Then

Begin

Try

Val (Inputedit.text,res,code);

If Code <> 0 Then

Raise Einvalidinput.create (1);

if (res > 1) or (res < 0) Then

Raise Einvalidinput.create (2);

Messagedlg (' Correct Input ', Mtinformation,[mbok], 0);

Key: = #0;

Except

On E:einvalidinput do

Begin

Inputedit.text: = ';

Messagedlg (E.message, Mtwarning,[mbok], 0);

End

End

End

End

Because it is cleared after an exception response, additional means are required to display the exception information. In the above two procedures we use two different methods: in the password legality check, use the exception to trigger the default response by the system, in the input digital legitimacy check, through the exception instance to get the exception information and display it by themselves.

The above is a very simple example, but it can be found that using custom exception programming provides great flexibility for programming.

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.