You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 12, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: pages/book/statements.mdx
+71-4Lines changed: 71 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -79,9 +79,9 @@ Control the flow of the code.
79
79
80
80
</Callout>
81
81
82
-
When executing an `if...else{:tact}` statement, first, the specified condition gets evaluated. If the resulting value is `true{:tact}`, the `then{:tact}` statement block gets executed. Otherwise, if the condition evaluates to `false{:tact}`, the optional `else{:tact}` statement block will be executed. If the `else{:tact}` block is missing, nothing happens and execution continues further.
82
+
When executing an `if...else{:tact}` statement, first, the specified condition gets evaluated. If the resulting value is `true{:tact}`, the following statement block gets executed. Otherwise, if the condition evaluates to `false{:tact}`, the optional `else{:tact}` block will be executed. If the `else{:tact}` block is missing, nothing happens and execution continues further.
83
83
84
-
Regular `if{:tact}`-statement:
84
+
Regular `if{:tact}`statement:
85
85
86
86
```tact
87
87
// condition
@@ -91,7 +91,7 @@ if (true) { // consequence, when condition is true
91
91
}
92
92
```
93
93
94
-
With `else{:tact}`clause:
94
+
With `else{:tact}`block:
95
95
96
96
```tact
97
97
// condition
@@ -130,13 +130,78 @@ if (2 + 2 == 3) {
130
130
131
131
</Callout>
132
132
133
+
### `try...catch`[#try-catch]
134
+
135
+
The `try...catch{:tact}` statement is comprised of a `try{:tact}` block and an optional `catch{:tact}` block, which receives an [`Int{:tact}`][int][exit code](/book/exit-codes) as its only argument. The code in the `try{:tact}` block is executed first, and if it fails, the code in the `catch{:tact}` block will be executed and changes made in `try{:tact}` block will be rolled back, if possible.
136
+
137
+
<Callout>
138
+
139
+
Note, that some TVM state parameters, such as codepage and gas counters, will not be rolled back. That is, all gas usage in the `try{:tact}` block will be taken into account and the effects of opcodes that change the gas limit will be preserved.
140
+
141
+
</Callout>
142
+
143
+
Regular `try{:tact}` statement:
144
+
145
+
```tact
146
+
fun braveAndTrue() {
147
+
// Lets try and do something erroneous
148
+
try {
149
+
nativeThrow(42); // throwing with exit code 42
150
+
}
151
+
152
+
// The following will be executed as the erroneous code above was wrapped in a try block
153
+
dump(42);
154
+
}
155
+
```
156
+
157
+
With `catch (e){:tact}` block:
158
+
159
+
```tact
160
+
fun niceCatch() {
161
+
// Lets try and do something erroneous
162
+
try {
163
+
nativeThrow(42); // throwing with exit code 42
164
+
} catch (err) {
165
+
dump(err); // this will dump the exit code catched, which is 42
166
+
}
167
+
}
168
+
```
169
+
170
+
With nested `try...catch{:tact}`:
171
+
172
+
```tact
173
+
try {
174
+
// Preparing an x equal to 0, in such a way that Tact compiler won't realize it (yet!)
175
+
let xs: Slice = beginCell().storeUint(0, 1).endCell().beginParse();
176
+
let x: Int = xs.loadUint(1); // 0
177
+
178
+
try {
179
+
throw(101); // 1. throws with exit code 101
180
+
} catch (err) { // 2. catches the error and captures its exit code (101) as err
181
+
return err / x; // 3. divides err by x, which is zero, throwing with exit code 4
182
+
}
183
+
184
+
} catch (err) { // 4. catches the new error and captures its exit code (4) as err
185
+
// ^^^ this works without name collisions because the previous err
186
+
// has a different scope and is only visible inside the previous catch block
187
+
188
+
dump(err); // 5. dumps the last caught exit code (4)
189
+
}
190
+
```
191
+
192
+
<Callout>
193
+
194
+
Read more about exit codes on the dedicated page: [Exit codes in the Book](/book/exit-codes).
195
+
196
+
</Callout>
197
+
133
198
## Loops
134
199
135
200
Conditionally repeat certain blocks of code multiple times.
136
201
137
202
### `repeat`[#repeat-loop]
138
203
139
-
The `repeat{:tact}` loop executes a block of code a specified number of times. Number of repetitions must be a non-negative $32$-bit [`Int{:tact}`](/book/integers). Otherwise an error with the [exit code 5](/book/exit-codes#5), `Integer out of the expected range` would be thrown.
204
+
The `repeat{:tact}` loop executes a block of code a specified number of times. Number of repetitions must be a non-negative $32$-bit [`Int{:tact}`][int]. Otherwise an error with the [exit code 5](/book/exit-codes#5), `Integer out of the expected range` would be thrown.
140
205
141
206
In the following example, code inside the loop will be executed $10$ times:
142
207
@@ -222,3 +287,5 @@ dump(sum); // 1000
222
287
For additional loop examples see: [Loops in Tact-By-Example](https://tact-by-example.org/04-loops).
0 commit comments