[Solved] "try ... except on" syntax lazarus / 21-General ---------------------------------------------------------------------- Edson · 2023-09-19 20:47 Can someone, please, explain what is the logic of 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     try      ...    except      on EConvertError do Writeln('Invalid number');    on EDivByZero do Writeln('By zero');      else begin Writeln('Other error'); end;      //When a EConvertError or EDivByZero is fired, this doesn't execute.      //When other is fired, this execute.      writeln('Was error');    end;  First of all: Is legal to include some code after a "on ... do " sentence? And it's strange for me that when other exception is fired, the screen shows: Quote      Other error       Was error ---------------------------------------------------------------------- bytebites · 2023-09-19 21:02 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 program Project1; uses sysutils;begintry   raise EConvertError.Create('');   except     on EConvertError do Writeln('Invalid number');     on EDivByZero do Writeln('By zero');     else begin Writeln('Other error'); end;     //When a EConvertError or EDivByZero is fired, this doesn't execute.     //When other is fired, this execute.     writeln('Was error');   end;end. QuoteInvalid number ---------------------------------------------------------------------- Paolo · 2023-09-19 21:11 Code after "on..do" specialize the action for the specific error raised. Else introduce the answer for the other cases not listed as "on..do", else introduces a list of statements, thus begin..end in your case is useless. If I am not wrong. ---------------------------------------------------------------------- Fibonacci · 2023-09-19 21:17 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 uses SysUtils; begin  try    raise EAccessViolation.Create('');    //raise EDivByZero.Create('');  except    on EConvertError do writeln('convert error'); //wont continue    on EDivByZero    do writeln('div by zero');   //wont continue    else; //else is required if "on" used, otherwise its Syntax Error     writeln('exception happened');    writeln('and execution continues');  end;   writeln('the end');  readln;end. Quoteexception happened and execution continues the end ---------------------------------------------------------------------- dbannon · 2023-09-20 00:06 A small point. The 'else' is not required after a 'on' statement, its just one way to have a block when any other exception gets caught. A better approach, especially while developing, is to just catch any remaining exception with "on Exception", that also allows you to capture and display the exception that was caught. Then, once you know what it is, you can give it its own "on .." line. 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      {$mode objfpc}    uses SysUtils;         begin      try        raise EAccessViolation.Create('Test');        //raise EDivByZero.Create('');      except        on EConvertError do writeln('convert error'); //wont continue        on EDivByZero    do writeln('div by zero');   //wont continue//        else; //else is required if "on" used, otherwise its Syntax Error//        writeln('exception happened');//        writeln('and execution continues');        on E: Exception do writeln('unknown exception happened : ', E.ClassName, ' ',  E.Message);      end;           writeln('the end');      readln;    end.Code: [Select]dbannon@dell:~/Pascal/CLI$ ./exception unknown exception happened : EAccessViolation Test the end ---------------------------------------------------------------------- alpine · 2023-09-20 07:49 Weird! It seems that all statements following the else gets executed in case the exception was not caught, e.g. 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   on ...  else;  stat1;  stat2;    on ...  else    stat1;  stat2;   on ...  else   begin    stat1;    stat2;  end;    But if you have on handler, you must have else to have more statements. On the other hand, you may have statements as long you don't have on handler(s). It is shown into the diagram in https://www.freepascal.org/docs- html/current/ref/refse119.html#x245-26900017.2 BTW, but only now I paid attention to it. I always thought there should be a compound statement after the else  as in e.g. if. Weird... ---------------------------------------------------------------------- dbannon · 2023-09-20 10:13 Yeah, I never use the "else" there (because capturing some info about the unexpected exception is always useful) but I sure would have expected a "begin..end" would be needed for multiple statements. On the other hand, we don't 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 try    begin        statement 1        statement 2    endexcept So, maybe a list after "else" without the begin .. end  makes sense, I suppose. Davo ---------------------------------------------------------------------- Warfley · 2023-09-20 13:50 Quote from: dbannon on September 20, 2023, 02:06:39 amA small point. The 'else' is not required after a 'on' statement, its just one way to have a block when any other exception gets caught. A better approach, especially while developing, is to just catch any remaining exception with "on Exception", that also allows you to capture and display the exception that was caught. Then, once you know what it is, you can give it its own "on .." line. Until someone really hates you and puts in a "raise TStringList.Create". Exceptions can be any object, that they inherit from Exception is just a convention. Infact you may want to write a program without relying on sysutils, then you would define your own exception classes. So to capture any exception use TObject as base case ---------------------------------------------------------------------- Edson · 2023-09-20 15:05 Quote from: alpine on September 20, 2023, 09:49:50 amWeird! It seems that all statements following the else gets executed in case the exception was not caught, e.g. 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   on ...  else;  stat1;  stat2;    on ...  else    stat1;  stat2;   on ...  else   begin    stat1;    stat2;  end;    But if you have on handler, you must have else to have more statements. On the other hand, you may have statements as long you don't have on handler(s). It is shown into the diagram in https://www.freepascal.org/docs- html/current/ref/refse119.html#x245-26900017.2 BTW, but only now I paid attention to it. I always thought there should be a compound statement after the else  as in e.g. if. Weird... Yes. That's the explanation. The syntax definition allows just a sentence (or block) after ON ... DO, however allows a list of sentences after ELSE ... until the delimiter END. That's an ugly syntax for ELSE (like in the CASE OF), considering the ELSE for IF works in a different way. The syntax definition also answer my other question Quote from: Edson on September 19, 2023, 10:47:55 pmFirst of all: Is legal to include some code after a "on ... do " sentence? No Edson, it's not legal include code after ON sentence.