Recent

Author Topic: [SOLVED] Struggling to implement exception/error handling...  (Read 2822 times)

GypsyPrince

  • Guest
I am working on a beginner's tutorial from 'Modern Object Pascal Introduction for Programmers' by Michalis Kamburelis. I am having difficulty getting the Try... Exception statement to work correctly. Preview the following code:

Program tutorial_02;
    {$mode objfpc}{$H+}{$J-}

Uses
    {$IFDEF UNIX}{$IFDEF UseCThreads}
      cthreads,
    {$ENDIF}{$ENDIF}
      Classes

    { add any units after this };

Var
    sglTest: Single;

Procedure TestProc(Const MyNum: Integer);
Begin
    WriteLn('MyNumber + 10 is: ', MyNum + 10);
End;

Function TestFunc(Const MyTxt: String): String;
Begin
    Result := MyTxt + 'Strings are automatically managed';
End;

Begin
    Try
        Try
            WriteLn(TestFunc('Note: '));
            TestProc(5);
            sglTest := 15 / 5;                                 //Division using "/" always makes float result - use "div" for integer division...
            WriteLn('MyNum is now:', sglTest);        //Scientific notation...
            WriteLn('MyNum is now: ', sglTest:1:2); //2 decimal places...
        Except
            On E: Exception Do
            Begin
                WriteLn('An error occurred!');
            End;
        End;
    Finally
        {$IFDEF WINDOWS}
          ReadLn;
        {$ENDIF}
    End;
End.
 
The line "On E: Exception Do" produces the error {Error: Identifier not found "Exception"} when I attempt to compile the tutorial app. If I comment out this one line, the program runs perfectly.
 
Does anyone spot any thing I'm doing wrong in this simple example? Syntax error? Am I leaving out a unit required for exception handling?
 
Please help!
Thanks in advance!!
« Last Edit: June 30, 2018, 08:40:25 pm by GypsyPrince »

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Struggling to implement exception/error handling...
« Reply #1 on: June 20, 2018, 02:08:20 am »
Does anyone spot any thing I'm doing wrong in this simple example? Syntax error? Am I leaving out a unit required for exception handling?

Add SysUtils to your uses. Exception class is defined there.

https://www.freepascal.org/docs-html-3.0.0/rtl/sysutils/index-4.html

GypsyPrince

  • Guest
Re: Struggling to implement exception/error handling...
« Reply #2 on: June 20, 2018, 03:23:25 am »
That was it. You are the man!

Thanks!!!

 

TinyPortal © 2005-2018