Recent

Author Topic: Need help with "Try - Except" error handling.  (Read 1133 times)

uart

  • Jr. Member
  • **
  • Posts: 58
Need help with "Try - Except" error handling.
« on: January 17, 2020, 11:51:35 am »
Hi, new member here.

I've never used the exception handling "try except" construction before, but I would like to use it to trap certain numeric errors in some code that I want to write.

This Delphi help page, http://www.delphibasics.co.uk/RTL.asp?Name=try&ExpandCode3=Yes#Ex3, gives the following example code for trapping divide by zero, but I can't even get this simple example to work. (Tried in both Delphi 7 and in Lazarus with the same results).

In each case it just seems to ignore my error handler and go straight to the default error trap that terminates the program with - project1 raised the exception class 'External: SIGPFE'

BTW, the following is supposed to be a "generic" exception that should match anything. I know that there are more specific exceptions for divide by zero, but at this stage I'm just trying to get the simplest possible example working, and I can proceed from there.

Code: [Select]
var
  number, zero : Integer;
begin
  // Try to divide an integer by zero - to raise an exception
  Try
    zero   := 0;
    number := 1 div zero;
    ShowMessage('number / zero = '+IntToStr(number));
  except
    On E : Exception do
      ShowMessage(E.ClassName+' error raised, with message : '+
                  E.Message);
  end;
end;
« Last Edit: January 17, 2020, 12:09:15 pm by uart »

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Need help with "Try - Except" error handling.
« Reply #1 on: January 17, 2020, 11:56:50 am »
I each case it just seems to ignore my error handler and go straight to the default error trap that terminates the program with - project1 raised the exception class 'External: SIGPFE'
Your error handler isn't skipped. But you are probably running in the IDE and with debug-mode on and in that case the exception handler from the debugger is triggered first. You can press continue in which case your own error-code will be executed.

You can run your program without debugging (Run > Run without Debugging or Shift + Ctrl + F9) in which case the debugger-handler isn't triggered.

You can also specify certain exceptions for which the debugger-handler shouldn't be triggered (Tools > Options > Debugger > Language Exceptions).

uart

  • Jr. Member
  • **
  • Posts: 58
Re: Need help with "Try - Except" error handling.
« Reply #2 on: January 17, 2020, 11:59:19 am »
Good suggestion rvk. I will test it outside the IDE and report back.

uart

  • Jr. Member
  • **
  • Posts: 58
Re: Need help with "Try - Except" error handling.
« Reply #3 on: January 17, 2020, 12:08:26 pm »
Ok all good. Tested and working as expected in both Delphi and Lazarus.  :)

Now I can look at starting my project.

nanobit

  • Full Member
  • ***
  • Posts: 160
Re: Need help with "Try - Except" error handling.
« Reply #4 on: January 17, 2020, 12:40:00 pm »
If you run in Lazarus debug mode,
you can still show your own except-message after the debugger message.
If you don't see your own message, you have hit some IDE limit.
If you are on win32 (alike your Delphi), you can solve this by win32Veh in your uses clause.
https://forum.lazarus.freepascal.org/index.php/topic,44905.0.html

 

TinyPortal © 2005-2018