Recent

Author Topic: If - Then - Else and Try - Except  (Read 8374 times)

Odanka

  • New Member
  • *
  • Posts: 40
If - Then - Else and Try - Except
« on: August 16, 2018, 06:38:03 am »
I have code when SomeVar1 = 0 and SomeVar2 >= 0:
Code: Pascal  [Select][+][-]
  1. if SomeVar1 > 0 then
  2.    SomeVar3 := SomeVar2 / SomeVar1
  3. else
  4.   SomeVar3 := 0;  
  5.  
The above code everything it's ok

But here raise an exception error:
Code: Pascal  [Select][+][-]
  1. try
  2.    SomeVar3 := SomeVar2 / SomeVar1
  3. except
  4.   SomeVar3 := 0;  
  5.  

Exactly this is no problem at all, but anyone have experienced, please... :)


eric

  • Sr. Member
  • ****
  • Posts: 267
Re: If - Then - Else and Try - Except
« Reply #1 on: August 16, 2018, 08:04:58 am »
Code: [Select]
try
   SomeVar3 := SomeVar2 / SomeVar1
except
  on E: Exception do
    SomeVar3 := 0; 
end;

Odanka

  • New Member
  • *
  • Posts: 40
Re: If - Then - Else and Try - Except
« Reply #2 on: August 16, 2018, 08:27:58 am »
Code: [Select]
{
if Jumlah05 > 0 then
   OleData := Format('%0.2n',[(Jumlah01 - Jumlah05) / Jumlah05 * 100])
else
   OleData := 0;
}

try
   OleData := Format('%0.2n',[(Jumlah01 - Jumlah05) / Jumlah05 * 100])
except
   on E:Exception do
     OleData := 0;
end;
Still raise an exception error "External: SIGFPE"

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: If - Then - Else and Try - Except
« Reply #3 on: August 16, 2018, 08:41:36 am »
You have several options to 'hide' the exception warning:

1. Don't run it from the IDE
Use your file manager, browse to the exe file and double click it.

2. Run (from the IDE) without debugging
Lazarus main menu > Run > Run without Debugging.

3. Or use this code:
Code: Pascal  [Select][+][-]
  1.   if (SomeVar1 <> 0) then
  2.     SomeVar3 := SomeVar2 / SomeVar1
  3.   else
  4.     SomeVar3 := 0;

Odanka

  • New Member
  • *
  • Posts: 40
Re: If - Then - Else and Try - Except
« Reply #4 on: August 16, 2018, 08:53:28 am »
Solved by Run Without Debugging and work properly.
Thank you :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: If - Then - Else and Try - Except
« Reply #5 on: August 16, 2018, 11:03:09 am »
Well, to my opinion it is NOT solved:
What is somevar? An integer? Then you should use 'div' instead of '/'.
Pascal is not C, although C has pretentions...
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: If - Then - Else and Try - Except
« Reply #6 on: August 16, 2018, 02:50:34 pm »
Also, you should catch specific exceptions rather than the generic Exception:

Code: Pascal  [Select][+][-]
  1. try
  2.   somevar3 := somevar2 / maybezero;
  3. except
  4.   on EDivideByZero do
  5.     somevar3 := 0;
  6.   end;
  7. end;

Just in case something else happened that you should be aware of.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: If - Then - Else and Try - Except
« Reply #7 on: August 16, 2018, 03:25:36 pm »
Also, you should catch specific exceptions rather than the generic Exception:

Code: Pascal  [Select][+][-]
  1. try
  2.   somevar3 := somevar2 / maybezero;
  3. except
  4.   on EDivideByZero do
  5.     somevar3 := 0;
  6.   end;
  7. end;

Just in case something else happened that you should be aware of.
Yes, but if you use *specified* exceptions do it like this:
Code: Pascal  [Select][+][-]
  1. try
  2.   somevar3 := somevar2 / maybezero;
  3. except
  4.   on EDivByZero do
  5.     somevar3 := 0
  6.   else begin
  7.     {$ifopt D+}Showmessage(e.message);{$endif} // what's all this then?
  8.     Raise; // if it is NOT a division by zero, it is something else, so re-raise...Hopefully your program catches it elsewhere....
  9.     end;
  10.   end;
  11. end;
Or like this:
Code: Pascal  [Select][+][-]
  1. try
  2.   somevar3 := somevar2 / maybezero;
  3. except
  4.   on EDivByZero do
  5.     somevar3 := 0
  6.   else
  7.     Application.HandleException; // a (semi-)safe way to terminate in worst case scenario's in a Lazarus program (abort.. continue)
  8.   end;
  9. end;
With the use of *specified* exceptions come responsibilities....
Reason: How can you catch what you do not name? In the best case the exception (other than a EDivByZero)  will get eaten (ignored) in the worst case you just wrote code that you can hardly debug.
Production code is often more complex than this, but hardly, very limited,  (should) use exceptions. The matter is really not trivial. Programmers think it is trivial, but it is not!

Rule book:
1. What can cause an exception here? (If you KNOW that, you don't need an exception handler, but proper code!)
2. Can I mitigate without brute force? (using exceptions is brute force!)
3. If so, act like a programmer: solve it without exception use.
4. If you can NOT predict the exception, then use an exception handler during development. Observe what happens and goto 2)
5. If all else fails, you are allowed to use exceptions in production code...Because they will be true exceptions...And won't occur much...if at all...

Example: testing the divisor for zero 1.000.000 times (one cycle on most processors) is usually cheaper than handling EDivByZero just 1 time, if you know it is going to happen beforehand.
Since you named your divisor "maybezero" you just disqualified yourself to use exceptions in any meaningful way.... :D 8-) O:-)
BTW: Handoko showed above how to solve this properly.
« Last Edit: August 16, 2018, 05:02:37 pm by Thaddy »
Specialize a type, not a var.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: If - Then - Else and Try - Except
« Reply #8 on: August 16, 2018, 07:21:00 pm »
@Thaddy: You are absolutely right in most everything you say. My plea re the (dis-)use of exceptions is that I was following the lead of the OP. I *do* follow your "rule book"; after all I'm not a newbie and I *do* know (by bitter experience, if you will) how true is that thing about the worth of "an ounce of prevention".

Re my example ... I can aduce nothing for its slopiness but haste, which is not a good excuse, I know :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: If - Then - Else and Try - Except
« Reply #9 on: August 16, 2018, 09:42:24 pm »
Solved by Run Without Debugging and work properly.
Exceptions are used for unexpected behavior. You expect a zero value. So just code the test. It's faster and clearer.
Using an exception to check for zero is a bad style.

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: If - Then - Else and Try - Except
« Reply #10 on: August 17, 2018, 10:44:00 am »
Not only bad style, but also resource wasting.  TRY ...END needs more memory and CPU resources than IF ... THEN.  Don't use TRY ... END unless you need it.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Odanka

  • New Member
  • *
  • Posts: 40
Re: If - Then - Else and Try - Except
« Reply #11 on: August 21, 2018, 03:39:54 am »
Well, to my opinion it is NOT solved:
What is somevar? An integer? Then you should use 'div' instead of '/'.
Pascal is not C, although C has pretentions...

Thank You Thaddy, here somevar is Double

Odanka

  • New Member
  • *
  • Posts: 40
Re: If - Then - Else and Try - Except
« Reply #12 on: August 21, 2018, 03:44:30 am »
I'm happy to get some solution, opinion from all of you. Thank you very much. :)

 

TinyPortal © 2005-2018