on try..except.. lazarus / 21-General ---------------------------------------------------------------------- Paolo · 2023-03-17 14:43 Hello, I see that a common action can be used for all possible exception writing code like this (the code is meaninful just to illustrate the concept) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button2Click(Sender: TObject);var  a, b, c : double;begin  try    a:=1;    b:=0;    c:=a/b;  except    MessageDlg('Something went wrong', mtError, [mbOK], 0);  end;end;  I see that except section allows specialize the answer like this (Still covering all the possible exception thanks to “else”) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.Button3Click(Sender: TObject);var  a, b, c : double;begin  try    a:=1;    b:=0;    c:=a/b;  except    on E:EZeroDivide do      MessageDlg('Division by 0', mtError, [mbOK], 0);    on E:EMathError do      MessageDlg('Computational Error', mtError, [mbOK], 0);    else      MessageDlg('Something unexpected went wrong', mtError, [mbOK], 0);  end;end;   Now my question: how implement an exception section that do something in ”all the cases” but then lefts the specialized case do the rest of the job? I would like something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  a, b, c : double;begin  try    a:=1;    b:=StrToFloat(Edit1.text);    c:=a/b;   except    //MessageDlg('Something went wrong', mtError, [mbOK], 0); <-- code needed for all exception cases, it does not compile    on E:EZeroDivide do      MessageDlg('Division by 0', mtError, [mbOK], 0);    on E:EMathError do      MessageDlg('Computational Error', mtError, [mbOK], 0);    on E:EConvertError do       MessageDlg('Conversion Error', mtError, [mbOK], 0);  end;end;  How can I achieve this behaviour ? are there other ways to achieve this ? Thank you ---------------------------------------------------------------------- PascalDragon · 2023-03-17 14:52 Quote from: Paolo on March 17, 2023, 03:43:19 pmNow my question: how implement an exception section that do something in ”all the cases” but then lefts the specialized case do the rest of the job? I would like something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  a, b, c : double;begin  try    a:=1;    b:=StrToFloat(Edit1.text);    c:=a/b;   except    //MessageDlg('Something went wrong', mtError, [mbOK], 0); <-- code needed for all exception cases, it does not compile    on E:EZeroDivide do      MessageDlg('Division by 0', mtError, [mbOK], 0);    on E:EMathError do      MessageDlg('Computational Error', mtError, [mbOK], 0);    on E:EConvertError do       MessageDlg('Conversion Error', mtError, [mbOK], 0);  end;end;  How can I achieve this behaviour ? are there other ways to achieve this ? One of the simpler solutions would be to add a nested exception handler block: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  a, b, c : double;begin  try    try      a:=1;      b:=StrToFloat(Edit1.text);      c:=a/b;    except      MessageDlg('Something went wrong', mtError, [mbOK], 0); // <-- code needed for all exception cases, it does not compile      raise;    end;  except    on E:EZeroDivide do      MessageDlg('Division by 0', mtError, [mbOK], 0);    on E:EMathError do      MessageDlg('Computational Error', mtError, [mbOK], 0);    on E:EConvertError do       MessageDlg('Conversion Error', mtError, [mbOK], 0);  end;end; ---------------------------------------------------------------------- Paolo · 2023-03-17 15:28 that was my fisrt idea, thanks ---------------------------------------------------------------------- Warfley · 2023-03-17 17:19 Exceptions are always class instances, so you can do this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure ExceptionTest(toRaise: TObject);begin  try    raise toRaise;  except on E: TObject do  begin    WriteLn('Exception');    if E is EConvertError then      WriteLn('Convert Error: ' + Exception(E).Message)    else if E is Exception then      WriteLn(Exception(E).Message);  end;  end;end; begin  ExceptionTest(EConvertError.Create('Convert')); // Exception + Convert Error Convert  ExceptionTest(EFormatError.Create('Format'));  // Exception + FormatIn most cases the Exception will inherit from the Exception type from SysUtils, so you could even use this rather than TObject Alternatively you might use AcquireExceptionObject, but then you extract that and have to ReleaseExceptionObject it afterwards ---------------------------------------------------------------------- Thaddy · 2023-03-17 17:32 I am missing in all answers the quite essentialCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  else raise; ---------------------------------------------------------------------- Paolo · 2023-03-18 09:49 thanks to all for the suggestions