Dear enlighted ones.

I happen to try to finsh a kind of a function library that my Main program can utilize.
As of now, Everything work great... but. I would like to implement some kind of error handling.
The way I do this also work, but the error is generated so that the procedure detecting the error is listed when Delphi breaks execution.
Is there some way of implementing something so that the Break shows the line in main program instead?
It is important to me to make descriptive error messages that can be used to solve problem.

//From Main program
PROCEDURE TestForError;
  BEGIN
    Example(6);
    Example(8);
    Example(10);
    Example(15); // I would like Delphi to break here, and show a nice red line.
    Example(7);
    Example(1);
  END;

//From Library Unit
PROCEDURE Example(Max10:BYTE);
  VAR
    ErrorMessage:STRING;
  BEGIN
    IF Max10>10 THEN BEGIN
      ErrorMessage='An error occurred. Parameter can not hold larger values than 10';
      RAISE Exception.Create(ErrorMessage) at @Example;
    END;
  END;

I thought I could use something like this, but I need some kind of more information.
This way of raising an exception does everything I would like, except it show my LibraryUnit and procedure there as the cause of the error. This is of course correct enough as it is there the error is detected. I would just like to redirect the error so that the line calling the procedure get the "fault", hence Delphi breaks, showing this line of code instead of showing my procedure in library unit.

Also, what if many procedures in my library is used in order to determine that there is an error. I would still like only the line in Main program to be shown as red after Delphi break execution.

Many thanks in advance for any help regarding this issue.:)

You can use a try-except block like so:

try
  begin
    Example(6);
    Example(8);
    Example(10);
    Example(15);
    Example(7);
    Example(1);
  end
except on E: Exception do
  raise Exception.Create(E.Message); // raised as a new exception so will include the line calling Example(15) as the error line
end;

I suppose that would work, but that also mean that I have to use an exception handler in every part of my main program where errors might occur.
It would lead to a lot of extra code and make it more difficult to read.

It is a solution, but I want it to be handled outside of main program :-)
Thanks for the suggestion anyway.

A friend of mine seem to have found a kind of solution to my problem.
If my unit is compiled with the

{$DebugInfo Off}

Compiler directive, then if the raise exception part of code references one of the parameters in the call, then the exception is 'raised' or shown in main part of program by delphi when responding to the error.
It seems like it target the line after the actual line causing the triggered exception, but fair enough, and close enough for me :-)

I simply put the directive right after my units Uses clause, and problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.