After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 546043 - Exception throwing function call is used as an argument to function call, exception not caught
Exception throwing function call is used as an argument to function call, exc...
Status: RESOLVED DUPLICATE of bug 475922
Product: vala
Classification: Core
Component: Code Generator
unspecified
Other All
: Normal normal
: ---
Assigned To: Jürg Billeter
Vala maintainers
Depends on:
Blocks:
 
 
Reported: 2008-08-02 23:26 UTC by Stef Walter
Modified: 2008-09-10 16:27 UTC
See Also:
GNOME target: ---
GNOME version: ---



Description Stef Walter 2008-08-02 23:26:17 UTC
When an function that throws an exception is used as an argument to another function, any exceptions thrown are not caught. ie:

try {
    some_function(throws_exception());
} catch(GLib.Error err) {
    /* never reached */
}

Here's a full demonstration:


/* valac test.vala */

class Test {
	static int blah(int test) throws GLib.Error {
		if (test > 0)
			throw new GLib.Error(GLib.Quark.from_string("test"), 0, "test error");
		return 5;
	}

	static void something(int val) {
		stdout.printf("value: %d\n", val);
	}

	static void main(string[] args) {
		try {
			something(blah(1));
		} catch(GLib.Error err) {
			stdout.printf("error: %s", err.message);
		}
	}
}
Comment 1 Cliff Brake 2008-09-06 21:28:03 UTC
confirmed (vala 0.3.5)

I have the following Vala code:

using GLib;

public errordomain TestError {
   FAILED
}

static bool test_func() throws TestError
{
   debug ("test function");
   throw new TestError.FAILED ("test error");
   return  true;
}

void main(string[] args)
{
   try {
       bool ret = test_func();
       assert(ret);
   } catch (TestError e) {
       debug ("this message is printed %s", e.message);
   }

   try {
       assert(test_func());
   } catch (TestError e) {
       debug ("this message never gets printed, even though the
exception is handled %s", e.message);
   }
}

Which produces the following output:
cbrake@happy:/build/tmp/vala_stat$ ./test_Error
** (process:14891): DEBUG: test_Error.vala:10: test function
** (process:14891): DEBUG: test_Error.vala:21: this message is printed
test error
** (process:14891): DEBUG: test_Error.vala:10: test function
**
** ERROR:(test_Error.c:61):_main: assertion failed: (test_func (&inner_error))
Aborted


So, it seems that if I wrap a function that can throw an exception in
assert(), the exception code does not get generated properly:

below is the C code:

static void _main (char** args, int args_length1) {
       GError * inner_error;
       inner_error = NULL;
       {
               gboolean ret;
               ret = test_func (&inner_error);
               if (inner_error != NULL) {
                       if (inner_error->domain == TEST_ERROR) {
                               goto __catch0_test_error;
                       }
                       g_critical ("file %s: line %d: uncaught error: %s", __FILE__,
__LINE__, inner_error->message);
                       g_clear_error (&inner_error);
               }
               g_assert (ret);
       }
       goto __finally0;
       __catch0_test_error:
       {
               GError * e;
               e = inner_error;
               inner_error = NULL;
               {
                       g_debug ("test_Error.vala:21: this message is printed %s", e->message);
                       (e == NULL ? NULL : (e = (g_error_free (e), NULL)));
               }
       }
       __finally0:
       ;
       {
               g_assert (test_func (&inner_error));
       }
       goto __finally1;
       __catch1_test_error:
       {
               GError * e;
               e = inner_error;
               inner_error = NULL;
               {
                       g_debug ("test_Error.vala:27: this message never gets printed, even
though the exception is handled %s", e->message);
                       (e == NULL ? NULL : (e = (g_error_free (e), NULL)));
               }
       }
       __finally1:
       ;
}
Comment 2 Jürg Billeter 2008-09-10 16:27:30 UTC
Thanks for the bug report. This particular bug has already been reported into our bug tracking system, but please feel free to report any further bugs you find.


*** This bug has been marked as a duplicate of 475922 ***