Recent

Author Topic: [Solved] "try ... except on" syntax  (Read 1301 times)

Edson

  • Hero Member
  • *****
  • Posts: 1314
[Solved] "try ... except on" syntax
« on: September 19, 2023, 10:47:55 pm »
Can someone, please, explain what is the logic of this?

Code: Pascal  [Select][+][-]
  1.     try
  2.         ...
  3.     except
  4.       on EConvertError do Writeln('Invalid number');
  5.       on EDivByZero do Writeln('By zero');
  6.       else begin Writeln('Other error'); end;
  7.       //When a EConvertError or EDivByZero is fired, this doesn't execute.
  8.       //When other is fired, this execute.
  9.       writeln('Was error');
  10.     end;
  11.  

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
« Last Edit: September 20, 2023, 07:32:16 pm by Edson »
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

bytebites

  • Hero Member
  • *****
  • Posts: 680
Re: "try ... except on" syntax
« Reply #1 on: September 19, 2023, 11:02:59 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses sysutils;
  4. begin
  5. try
  6.    raise EConvertError.Create('');
  7.    except
  8.      on EConvertError do Writeln('Invalid number');
  9.      on EDivByZero do Writeln('By zero');
  10.      else begin Writeln('Other error'); end;
  11.      //When a EConvertError or EDivByZero is fired, this doesn't execute.
  12.      //When other is fired, this execute.
  13.      writeln('Was error');
  14.    end;
  15. end.
  16.  

Quote
Invalid number

Paolo

  • Hero Member
  • *****
  • Posts: 538
Re: "try ... except on" syntax
« Reply #2 on: September 19, 2023, 11:11:03 pm »
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.
« Last Edit: September 19, 2023, 11:14:38 pm by Paolo »

Fibonacci

  • Hero Member
  • *****
  • Posts: 594
  • Internal Error Hunter
Re: "try ... except on" syntax
« Reply #3 on: September 19, 2023, 11:17:13 pm »
Code: Pascal  [Select][+][-]
  1. uses SysUtils;
  2.  
  3. begin
  4.   try
  5.     raise EAccessViolation.Create('');
  6.     //raise EDivByZero.Create('');
  7.   except
  8.     on EConvertError do writeln('convert error'); //wont continue
  9.     on EDivByZero    do writeln('div by zero');   //wont continue
  10.     else; //else is required if "on" used, otherwise its Syntax Error
  11.  
  12.     writeln('exception happened');
  13.     writeln('and execution continues');
  14.   end;
  15.  
  16.   writeln('the end');
  17.   readln;
  18. end.

Quote
exception happened
and execution continues
the end

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: "try ... except on" syntax
« Reply #4 on: September 20, 2023, 02:06:39 am »
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][+][-]
  1.      {$mode objfpc}
  2.     uses SysUtils;
  3.      
  4.     begin
  5.       try
  6.         raise EAccessViolation.Create('Test');
  7.         //raise EDivByZero.Create('');
  8.       except
  9.         on EConvertError do writeln('convert error'); //wont continue
  10.         on EDivByZero    do writeln('div by zero');   //wont continue
  11. //        else; //else is required if "on" used, otherwise its Syntax Error
  12. //        writeln('exception happened');
  13. //        writeln('and execution continues');
  14.         on E: Exception do writeln('unknown exception happened : ', E.ClassName, ' ',  E.Message);
  15.       end;
  16.      
  17.       writeln('the end');
  18.       readln;
  19.     end.
Code: [Select]
dbannon@dell:~/Pascal/CLI$ ./exception
unknown exception happened : EAccessViolation Test
the end

Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

alpine

  • Hero Member
  • *****
  • Posts: 1291
Re: "try ... except on" syntax
« Reply #5 on: September 20, 2023, 09:49:50 am »
Weird!
It seems that all statements following the else gets executed in case the exception was not caught, e.g.
Code: Pascal  [Select][+][-]
  1.   on ...
  2.   else;
  3.   stat1;
  4.   stat2;
  5.  
  6.   on ...
  7.   else
  8.     stat1;
  9.   stat2;
  10.  
  11.   on ...
  12.   else
  13.   begin
  14.     stat1;
  15.     stat2;
  16.   end;  
  17.  

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...
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

dbannon

  • Hero Member
  • *****
  • Posts: 3156
    • tomboy-ng, a rewrite of the classic Tomboy
Re: "try ... except on" syntax
« Reply #6 on: September 20, 2023, 12:13:53 pm »
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][+][-]
  1. try
  2.     begin
  3.         statement 1
  4.         statement 2
  5.     end
  6. except

So, maybe a list after "else" without the begin .. end  makes sense, I suppose.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

Warfley

  • Hero Member
  • *****
  • Posts: 1742
Re: "try ... except on" syntax
« Reply #7 on: September 20, 2023, 03:50:45 pm »
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.

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

  • Hero Member
  • *****
  • Posts: 1314
Re: "try ... except on" syntax
« Reply #8 on: September 20, 2023, 05:05:43 pm »
Weird!
It seems that all statements following the else gets executed in case the exception was not caught, e.g.
Code: Pascal  [Select][+][-]
  1.   on ...
  2.   else;
  3.   stat1;
  4.   stat2;
  5.  
  6.   on ...
  7.   else
  8.     stat1;
  9.   stat2;
  10.  
  11.   on ...
  12.   else
  13.   begin
  14.     stat1;
  15.     stat2;
  16.   end;  
  17.  

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

First of all: Is legal to include some code after a "on ... do " sentence?

No Edson, it's not legal include code after ON sentence.
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

 

TinyPortal © 2005-2018