summaryrefslogtreecommitdiff
path: root/tests/errors/errors.vala
blob: 0029bff6cd13acbd9871d11d65a7db23de76839e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using GLib;

errordomain BarError {
	FOO,
	BAR
}

class Bar : Object {
	public void foo () throws BarError {
		stdout.printf (" 6");

		throw new BarError.FOO (" 8");

		assert_not_reached ();
	}

	public int bad () throws BarError {
		stdout.printf (" 5");

		foo ();

		assert_not_reached ();

		return 0;
	}

	public void good () throws BarError {
		stdout.printf (" 4");
	}

	public void error_cast_check (GLib.Error e) {}

	public void run () {
		stdout.printf (" 2");

		try {
			stdout.printf (" 3");

			good ();

			int i = bad ();

			good ();

			assert_not_reached ();
		} catch (BarError e) {
			stdout.printf (" 7");

			stdout.printf ("%s", e.message);

			stdout.printf (" 9");
		}

		stdout.printf (" 10");
	}

	public static void test_generic_catch () {
		try {
			throw new BarError.FOO ("error message");
		} catch (Error e) {
			if (e is BarError && e is BarError.FOO) {
				return;
			}
		}

		assert_not_reached ();
	}

	public static void test_try_without_error () {
		try {
		} catch (Error e) {
			assert_not_reached ();
		}
	}
}

void main () {
	stdout.printf ("Exception Test: 1");

	var bar = new Bar ();
	bar.run ();

	stdout.printf (" 11\n");

	Bar.test_generic_catch ();
	Bar.test_try_without_error ();
}