1 / 9

Understanding Visibility, Exceptions, and Error Handling in Pascal Programming

This guide covers the foundational concepts of unit visibility and exception handling in Pascal programming. It explains the structure of a unit file, including the interface and implementation sections, and delves into visibility specifiers: public, private, and protected. Additionally, it addresses runtime errors, how exceptions are raised, detected, and handled using the try...except structure, with sample code for clarity. Perfect for beginners seeking to understand the nuances of Pascal units and error management.

benito
Télécharger la présentation

Understanding Visibility, Exceptions, and Error Handling in Pascal Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 9. Visibility and Exceptions

  2. Unit Visibility • Basic structure of unit file: unit U1; interface implementation end. • Two sections • interface section: definitions visible to other units, via uses statement • implementation section: definitions not visible to other units

  3. Class Visibility • Three visibility specifier keywords • public: default, can be seen from outside unit, provided within interface section • private: cannot be seen from outside unit, even if in interface section • protected: covered in a following lecture topic mentioned in Williams and Walmsley (1999) p. 235

  4. Example UClassVis.pas unit UClassVis; interface type CL1 = class public a: integer; procedure SetB(); function GetB(): integer; private b: integer; end; implementation procedure CL1.SetB(); begin b := Random(20); end; function CL1.GetB(): integer; begin Result := b; end; end.

  5. Example UMain.pas uses ... UClassVis; implementation ... var thing: CL1; procedure TForm1.cmdButtonClick(Sender: TObject); begin thing.SetB(); lblB.Caption := thing.GetB(); end; procedure TForm1.FormCreate(Sender: TObject); begin thing := CL1.Create(); end; end.

  6. Exceptions • Exceptions are raised in response to run time errors, such as: • division by 0 • conversion errors • file errors • this type of error occurs as result of circumstances specific to each time program is executed – so cannot be prevented • however, they can be detected (trapped/caught), and handled • Williams and Walmsley (1999) – chapter 11

  7. try … except structure • to detect and deal with errors: try … code that may experience run time errors except on E1 do … code to deal with error on E2 do … code to deal with error end;

  8. Example procedure TfrmErrors.cmdCalcClick(Sender: TObject); var Num1, Num2, Res: double; begin Num1 := StrToFloat(txtNum1.Text); Num2 := StrToFloat(txtNum2.Text); Res := Num1 / Num2; lblResult.Caption := FloatToStr(Res); end;

  9. Example procedure TfrmErrors.cmdCalcClick(Sender: TObject); var Num1, Num2, Res: double; begin try Num1 := StrToFloat(txtNum1.Text); Num2 := StrToFloat(txtNum2.Text); Res := Num1 / Num2; lblResult.Caption := FloatToStr(Res); except on EConvertError do ShowMessage('Can only do calculation with numbers'); on EZeroDivide do ShowMessage('Cannot divide by 0'); else raise; end; end;

More Related