mirror of
https://github.com/c3lang/c3c.git
synced 2026-02-27 12:01:16 +00:00
Add @try Builtin (#2333)
* Add @try macro * Add @try_catch. Update release notes. --------- Co-authored-by: Christoffer Lerno <christoffer@aegik.com>
This commit is contained in:
@@ -28,6 +28,41 @@ fn void test_enum_by_name()
|
||||
assert(@catch(enum_by_name(Tester, "GHI")) == NOT_FOUND);
|
||||
}
|
||||
|
||||
|
||||
faultdef SOME_FAULT, ABC_FAULT;
|
||||
|
||||
fn void test_try_catch()
|
||||
{
|
||||
int val;
|
||||
int? x = ABC_FAULT?;
|
||||
assert(@try_catch(val, x, ABC_FAULT)!!);
|
||||
assert(val == 0);
|
||||
assert(!@catch(@try_catch(val, x, ABC_FAULT)));
|
||||
x = SOME_FAULT?;
|
||||
assert(@catch(@try_catch(val, x, ABC_FAULT)) == SOME_FAULT);
|
||||
x = 3;
|
||||
assert(!@try_catch(val, x, ABC_FAULT)!!);
|
||||
assert(val == 3);
|
||||
}
|
||||
|
||||
fn void test_try_set()
|
||||
{
|
||||
assert(enum_by_name(Tester, "ABC")!! == Tester.ABC);
|
||||
|
||||
Tester val;
|
||||
if (catch @try(val, enum_by_name(Tester, "ABC"))) abort("Test failure");
|
||||
assert(val == Tester.ABC);
|
||||
|
||||
Tester another;
|
||||
if (catch err = @try(another, enum_by_name(Tester, "GHI")))
|
||||
{
|
||||
assert(err == NOT_FOUND);
|
||||
return;
|
||||
}
|
||||
|
||||
abort("Test failure");
|
||||
}
|
||||
|
||||
fn void test_likely()
|
||||
{
|
||||
int a = 2;
|
||||
|
||||
Reference in New Issue
Block a user