Re: catching exceptions



Hi,

On Wed, Apr 30, 2008 at 8:04 AM, muppet <scott asofyet org> wrote:

 On Apr 29, 2008, at 9:48 AM, Gabor Szabo wrote:


I must be missing something:

I am trying to get an iter on a line.

Sometimes the offset is too big so the below function throws
an exception:

$end_iter= $buffer->get_iter_at_line_offset($line, $end);

Gtk-ERROR **: Char offset 24 is off the end of the line at ....

The problem is that even if I put the expression in eval it does not
catch it.


 When it says "$somename-ERROR" like that, it's not really an exception, but
somebody calling g_error() down inside the C code.  These are considered
fatal error conditions.

 Sure enough:

 $ grep "is off the end of the line" gtk+/gtk/*.c
 gtk+/gtk/gtktextiter.c:    g_error ("Byte index %d is off the end of the
line",
 gtk+/gtk/gtktextiter.c:    g_error ("Char offset %d is off the end of the
line",

 Those are in the functions iter_set_from_byte_offset() and
iter_set_from_char_offset().

 The docs for gtk_text_buffer_get_iter_at_line_offset() say:

  * Obtains an iterator pointing to @char_offset within the given
  * line. The @char_offset must exist, offsets off the end of the line
  * are not allowed. Note <emphasis>characters</emphasis>, not bytes;
  * UTF-8 may encode one character as multiple bytes.




Can I catch this exception somehow?


 With some work, yes, you can trap it with a perl handler.  This is not
really generic, though, and is not a proper solution to your problem.

As a Perl programmer I think I should be able to trap every exception,
even if that
occurs in the C-level code beneath.

I think it would be better if the Perl binding would trap these and
throw them as
exceptions in the Perl code.

Please consider adding this to future releases of Gtk2.



As a workaround, can I get the length of a row from the Text::Buffer?


 /**
  * gtk_text_iter_get_chars_in_line:
  * @iter: an iterator
  *
  * Returns the number of characters in the line containing @iter,
  * including the paragraph delimiters.
  *
  * Return value: number of characters in the line
  **/

 Looks like your best bet will be

   $iter = $buffer->get_iter_at_line ($line_index);
   $chars_in_line = $iter->get_chars_in_line ();
   if ($chars > $chars_in_line) {
       $chars = $chars_in_line;
   }
   $iter->forward_chars ($chars);

While I actually already found a solution to the problem I had then
I am sure I'll encounter similar issues later.

Would it be possible to add the above convenience function to the standard
Gtk2 Perl distribution?

Thanks anyway

Gabor



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