12th Chapter-Exception Handling and program debugging (i) (2)

Source: Internet
Author: User

12.1.1.3 Integer exception

An integer exception is derived from a Einterror class, but it is always raised in the program's subclass: Edivbyzero,erangeerror,eintoverflow.

Table 12.2 Integer anomalies and their causes

━━━━━━━━━━━━━━━━━━━━━

Exception class causes

─────────────────────

Edivbyzero tried to be 0 apart

Erangeerror integer expression out of bounds

Eintoverflow integer Operation overflow

━━━━━━━━━━━━━━━━━━━━━━

Erangeerror is thrown when the value of an integer expression exceeds the range allocated for a particular integer type. For example, the following code throws a Erangeerror exception.

Var

Smallnumber:shortint;

X, Y:integer;

Begin

X: = 100;

Y: = 75;

Smallnumber: = X * Y;

End

Specific integer types include Shortint, Byte, and integer-compatible enumeration types, Boolean types, and so on. For example:

Type

Thazard = (Safety, marginal, Critical, catastrophic);

Var

Haz:thazard;

Item:integer;

Begin

Item:= 4;

haz:= Thazard (Item);

End

A Erangeerror exception is thrown because the enumerator is out of bounds.

Array elements that are out of bounds also raise a erangeerror exception, such as:

Var

VALUES:ARRAY[1..10] of Integer;

I:integer;

Begin

For I: = 1 todo

Values[i]: = i;

End

Erangeerror exceptions are raised only when type checking is open. This can include {$R +} Compile instructions or set IDE option| in code The Range_checking option selection box for project.

Eintoverflow an exception class is raised when the integer, Word, Longint, or three integral types are out of bounds. Such as:

Var

I:integer;

A,b,c:word;

Begin

A: = 10;

B: = 20;

c: = 1;

For I: = 0 Todo

Begin

c: = A*b*c;

End

End

Throws a Eintoverflow exception.

The Eintoverflow exception class is only option| in the compilation selection box Project| Over_flow_check option is selected before it is generated. When the overflow check is turned off, the variable retains the maximum range value of the class integer after the overflow.

The range of integer types is the following table.

Table 12.3 Range of integer types

━━━━━━━━━━━━━━━━━━━━━━━━━━━

Type range Format

───────────────────────────

Shortint-128.. 127 Signed 8-bit

Integer-32768.. 32767 signed 16-bit

Longint-2147483648.. 2147483647 signed 32-bit

Byte 0. 255 unsigned 8-bit

Word 0. 65535 unsigned 16-bit

━━━━━━━━━━━━━━━━━━━━━━━━━━━

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.