Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 5a82ca0

Browse files
authored
feat: try...catch statements (#223)
1 parent 95a2ab8 commit 5a82ca0

File tree

1 file changed

+71
-4
lines changed

1 file changed

+71
-4
lines changed

pages/book/statements.mdx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ Control the flow of the code.
7979

8080
</Callout>
8181

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.
8383

84-
Regular `if{:tact}`-statement:
84+
Regular `if{:tact}` statement:
8585

8686
```tact
8787
// condition
@@ -91,7 +91,7 @@ if (true) { // consequence, when condition is true
9191
}
9292
```
9393

94-
With `else{:tact}` clause:
94+
With `else{:tact}` block:
9595

9696
```tact
9797
// condition
@@ -130,13 +130,78 @@ if (2 + 2 == 3) {
130130

131131
</Callout>
132132

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+
133198
## Loops
134199

135200
Conditionally repeat certain blocks of code multiple times.
136201

137202
### `repeat` [#repeat-loop]
138203

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.
140205

141206
In the following example, code inside the loop will be executed $10$ times:
142207

@@ -222,3 +287,5 @@ dump(sum); // 1000
222287
For additional loop examples see: [Loops in Tact-By-Example](https://tact-by-example.org/04-loops).
223288

224289
</Callout>
290+
291+
[int]: /book/integers

0 commit comments

Comments
 (0)