• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/02_runtime/03_try_catch

  1 Wrapping an exeptional operation in try {} catch {} allows handling the
  2 resulting exception and to continue the execution flow.
  3 
  4 -- Expect stdout --
  5 Catched first exception.
  6 Catched second exception: exception 2.
  7 After exceptions.
  8 -- End --
  9 
 10 -- Testcase --
 11 {%
 12         // A try-catch block that discards the exception information.
 13         try {
 14                 die("exception 1");
 15         }
 16         catch {
 17                 print("Catched first exception.\n");
 18         }
 19 
 20         // A try-catch block that captures the resulting exception in
 21         // the given variable.
 22         try {
 23                 die("exception 2");
 24         }
 25         catch (e) {
 26                 print("Catched second exception: ", e, ".\n");
 27         }
 28 
 29         print("After exceptions.\n");
 30 %}
 31 -- End --
 32 
 33 
 34 Ensure that exceptions are propagated through C function calls.
 35 
 36 -- Expect stderr --
 37 exception
 38 In [anonymous function](), line 3, byte 18:
 39   called from function replace ([C])
 40   called from anonymous function ([stdin]:4:3)
 41 
 42  `        die("exception");`
 43   Near here -------------^
 44 
 45 
 46 -- End --
 47 
 48 -- Testcase --
 49 {%
 50         replace("test", "t", function(m) {
 51                 die("exception");
 52         });
 53 %}
 54 -- End --
 55 
 56 
 57 Ensure that exception can be catched through C function calls.
 58 
 59 -- Expect stdout --
 60 Caught exception: exception
 61 -- End --
 62 
 63 -- Testcase --
 64 {%
 65         try {
 66                 replace("test", "t", function(m) {
 67                         die("exception");
 68                 });
 69         }
 70         catch (e) {
 71                 print("Caught exception: ", e, "\n");
 72         }
 73 %}
 74 -- End --
 75 
 76 
 77 Ensure that exceptions are propagated through user function calls.
 78 
 79 -- Expect stderr --
 80 exception
 81 In a(), line 3, byte 18:
 82   called from function b ([stdin]:7:5)
 83   called from function c ([stdin]:11:5)
 84   called from anonymous function ([stdin]:14:4)
 85 
 86  `        die("exception");`
 87   Near here -------------^
 88 
 89 
 90 -- End --
 91 
 92 -- Testcase --
 93 {%
 94         function a() {
 95                 die("exception");
 96         }
 97 
 98         function b() {
 99                 a();
100         }
101 
102         function c() {
103                 b();
104         }
105 
106         c();
107 %}
108 -- End --
109 
110 
111 Ensure that exceptions can be caught in parent functions.
112 
113 -- Expect stdout --
114 Caught exception: exception
115 -- End --
116 
117 -- Testcase --
118 {%
119         function a() {
120                 die("exception");
121         }
122 
123         function b() {
124                 a();
125         }
126 
127         function c() {
128                 try {
129                         b();
130                 }
131                 catch (e) {
132                         print("Caught exception: ", e, "\n");
133                 }
134         }
135 
136         c();
137 %}
138 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt