Recent

Author Topic: [Solved] Where is EDatabaseError defined  (Read 11597 times)

dokhebi

  • New Member
  • *
  • Posts: 24
  • SysAdm, DBA, Programmer, SF/F Fan, Uber Whovian!
    • Ogre Musings
[Solved] Where is EDatabaseError defined
« on: December 11, 2012, 10:09:01 pm »
Using Laz 1.0.4 & FPC 2.6.0 on WinXP.

I want to catch an exception like this:

Code: [Select]
  try
    MySQL51Connection1.Connected := True;
  except
    On E: EDatabaseError do
      begin
        ShowMessage('Could not connect to database');
        Close;
      end;
  end;

but I get the following message:

Code: [Select]
main.pas(53,11) Error: Identifier not found "EDatabaseError"
main.pas(53,26) Error: class type expected, but got "<erroneous type>"
main.pas(69) Fatal: There were 2 errors compiling module, stopping

Where is EDatabaseError defined?  Is there a package/unit I need to include to make it work?

Thanks in advance.

Edward Hooper
« Last Edit: December 12, 2012, 08:15:04 pm by dokhebi »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 576
Re: Where is EDatabaseError defined
« Reply #1 on: December 11, 2012, 10:39:32 pm »
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/

dokhebi

  • New Member
  • *
  • Posts: 24
  • SysAdm, DBA, Programmer, SF/F Fan, Uber Whovian!
    • Ogre Musings
Re: Where is EDatabaseError defined
« Reply #2 on: December 11, 2012, 11:00:03 pm »
I included unit DB so it compiles, but it's not catching the exception.  Is there a property in the application or the control that states I will handle all exceptions?

Zoran

  • Hero Member
  • *****
  • Posts: 1459
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Where is EDatabaseError defined
« Reply #3 on: December 11, 2012, 11:39:17 pm »
I included unit DB so it compiles, but it's not catching the exception.  Is there a property in the application or the control that states I will handle all exceptions?

Perhaps the raised exception is not EDatabaseError?

Try something like this:
Code: [Select]
  try
    MySQL51Connection1.Connected := True;
  except
    on E: Exception do begin
      if E is EDatabaseError then
        ShowMessage('Could not connect to database')
      else
        ShowMessage('Raised Exception: ' + E.ClassName + LineEnding + 'with message:' + E.Message);
    end;
  end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Where is EDatabaseError defined
« Reply #4 on: December 12, 2012, 07:57:10 am »
You can also catch multiple exceptions like this (air code, typed in this post, not tested) - go from most specific exception to least specific:
Code: [Select]
  try
    MySQL51Connection1.Connected := True;
  except
// you can also do on C: E... database specific error (e.g. defined in ibconnection, mssqlconnection etc depending on which db you use. These often give some more detailed error messages)
    on D: EDatabaseError do begin
        ShowMessage('Database error is raised.')
    end;
    on E: Exception do begin
        ShowMessage('Raised Exception: ' + E.ClassName + LineEnding + 'with message:' + E.Message);
    end;
  end;
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

dokhebi

  • New Member
  • *
  • Posts: 24
  • SysAdm, DBA, Programmer, SF/F Fan, Uber Whovian!
    • Ogre Musings
Where is EDatabaseError defined
« Reply #5 on: December 12, 2012, 08:04:23 pm »
I found out what was happening.  The Lazarus environment runs the debugger by default so it catches the exception first.  I would click on Break in the pop-up message and the program would pause.  When I click on Continue it lets the exception handler code do its thing.  When I run the compiled binary outside of Lazarus it behaves as expected.

Thanks for the help.
« Last Edit: December 12, 2012, 08:08:25 pm by dokhebi »

BlueIcaro

  • Hero Member
  • *****
  • Posts: 576
Re: Where is EDatabaseError defined
« Reply #6 on: December 12, 2012, 09:47:25 pm »
I found out what was happening.  The Lazarus environment runs the debugger by default so it catches the exception first.  I would click on Break in the pop-up message and the program would pause.  When I click on Continue it lets the exception handler code do its thing.  When I run the compiled binary outside of Lazarus it behaves as expected.

Thanks for the help.
That  is the correct way that debuger works in Lazarus, and as i remenber in the version of  Delphi that I used.

/BlueIcaro
Remenber, the lazarus wiki is your friend: http://wiki.lazarus.freepascal.org/Main_Page
General questions (several lenguages) http://wiki.lazarus.freepascal.org/