Re: [Vala] Catch divide by zero



Andy,

AFAIK there is no way to do this in Vala, but you can always drop into
C and do it there.  Just add a *.c file with a function to do what you
want, then either add a VAPI or use the extern keyword to bind it, and
invoke the function from Vala.

That said, it's worth noting that division by 0 is undefined behavior
in C.  The SIGFPE-based solution you mentioned is POSIX only, and even
then it's not available on all architectures.  IIRC on Windows you're
supposed to use SEH instead of signals, not sure if they make any
guarantees about the functionality being available or not.

The right thing to do is just not divide by zero.  Checking to see if
the divisor is zero before performing the operation isn't particularly
expensive, especially if you use GLib.likely/unlikely to help on
architectures where branching is slow:

  if (GLib.likely(foo != 0))
    bar = baz / foo;
  else
    ...


-Evan



On Sun, 2016-12-04 at 17:22 +1100, Andy Lees wrote:
Is there a simple way to catch and propagate a divide by zero error
in
Vala?  In C I can use a setjmp to define a catch point, and then do a
suitable jongjmp from a signal handler.  However this mechanism
doesn't
appear to be available to the Vala programmer, so what can be done?

Regards,

Andrew Lees.
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]