Recent

Author Topic: Handling exception ... I'm missing something ...  (Read 2285 times)

andresayang

  • Full Member
  • ***
  • Posts: 108
Handling exception ... I'm missing something ...
« on: December 12, 2017, 02:37:53 pm »
Hi,

I have this code:
Code: Pascal  [Select][+][-]
  1.               try
  2.                 test:= Trunc(StrToFloat(strparser[0])*10);
  3.                 ProcessOneLine(strparser);
  4.               except
  5.                 on E:Exception do begin
  6.                 end;
  7.               end;
  8.  

strparser can contain the header of my file or the data.
When I run my program, it act as there was no execption handling ("Pki" is an invalid float).
I should mis something, any help ?
Thanks.
Linux, Debian 12
Lazarus: always latest release

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Handling exception ... I'm missing something ...
« Reply #1 on: December 12, 2017, 02:55:56 pm »
Sorry,

My mistake ... it works perfectly !

Linux, Debian 12
Lazarus: always latest release

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Handling exception ... I'm missing something ...
« Reply #2 on: December 12, 2017, 04:54:27 pm »
Sorry,

My mistake ... it works perfectly !
No it doesn't. You are hiding the exception and that can be done much easier (the stupid way like the above]:
Code: Pascal  [Select][+][-]
  1.              try
  2.                 test:= Trunc(StrToFloat(strparser[0])*10);
  3.                 ProcessOneLine(strparser);
  4.               except
  5.               end;
But that is not the way to handle exceptions. (Although this one doesn't need sysutils....) The above does the same as yours: Ostrich.

The simplest meaningful way is:
Code: Pascal  [Select][+][-]
  1.              try
  2.                 test:= Trunc(StrToFloat(strparser[0])*10);
  3.                 ProcessOneLine(strparser);
  4.               except
  5.                 on E:Exception do begin
  6.                    ShowMessage(E.Classname,'--',E.Message);                    
  7.               end;
That is still moronic, but at least a little more informative.
Either do not use EException at all or use exceptions that are inherited and more appropriate.

Note you can avoid my above remarks and collected KUDOS if you would have used TryStrToFloat in the first place.... 8-) :-X
       
 

 
« Last Edit: December 12, 2017, 05:00:43 pm by Thaddy »
Specialize a type, not a var.

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Handling exception ... I'm missing something ...
« Reply #3 on: December 12, 2017, 05:01:28 pm »
Hi,

Yes, I agree.
In my case, I only use the exception to do not process the header line of my file (if there is any).
So better to use

Code: Pascal  [Select][+][-]
  1. try
  2.   test:= StrToFloat(strparser[0]);
  3.   ProcessOneLine(strparser);
  4. except
  5. // I do not want to do anything here and I know which kind of exception it is
  6. // I also know that using exception to handle "normal way" of process, may not be the best solution
  7. // that's the reason why I change my code with test:= StrToFloatDef(strparser[0], -1);
  8. // if test > -1 then ProcessOneLine(strparser);
  9. end;
« Last Edit: December 12, 2017, 05:08:55 pm by andresayang »
Linux, Debian 12
Lazarus: always latest release

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Handling exception ... I'm missing something ...
« Reply #4 on: December 12, 2017, 05:09:06 pm »
Well, such things are in the Lazarus LCL code, but not in ANY FPC code...(compiler and RTL)
Never eat exceptions. You just THINK you may know which one it is, but you don't..
1. EOutOfMemory
2. EInOutError
3. EConvertError

Now.... which one do you THINK it is? All three can happen (more that just those 3).... Never eat exceptions, it is bad programmng. Always. Period.

Note StrToFloatDef is also mostly an Ostrich Approach that hides a mistake or unwanted result.
Better to use TryStrToFloat because the Boolean at least gives you a simple indication that the operation failed so you can debug it.
And it does not use exceptions. (Someone tried, got shot)
« Last Edit: December 12, 2017, 05:14:19 pm by Thaddy »
Specialize a type, not a var.

andresayang

  • Full Member
  • ***
  • Posts: 108
Re: Handling exception ... I'm missing something ...
« Reply #5 on: December 12, 2017, 05:17:20 pm »
Thanks for the "TryStrToFloat" tip, I do not know this function.
I using it now.
Linux, Debian 12
Lazarus: always latest release

Thaddy

  • Hero Member
  • *****
  • Posts: 14198
  • Probably until I exterminate Putin.
Re: Handling exception ... I'm missing something ...
« Reply #6 on: December 12, 2017, 05:31:40 pm »
 :D :D :D :D :D :D Good! Glad to be of help.
Specialize a type, not a var.

 

TinyPortal © 2005-2018