-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
228cf49
commit 3e508d3
Showing
3 changed files
with
45 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,44 @@ | ||
import ballerina/io; | ||
|
||
// Convert `bytes` to a `string` value and then to an `int` value. | ||
function intFromBytes(byte[] bytes) returns int|error { | ||
|
||
// Use `check` with an expression that may return `error`. | ||
// If `string:fromBytes(bytes)` returns an `error` value, `check` | ||
// makes the function return the `error` value here. | ||
// If not, the returned `string` value is used as the value of the `str` variable. | ||
function intFromBytesWithCheck(byte[] bytes) returns int|error { | ||
string str = check string:fromBytes(bytes); | ||
|
||
return int:fromString(str); | ||
} | ||
|
||
// same as intFromBytesWithCheck but with explicit error handling | ||
function intFromBytesExplicit(byte[] bytes) returns int|error { | ||
string|error res = string:fromBytes(bytes); | ||
if res is error { | ||
return res; | ||
} | ||
return int:fromString(res); | ||
} | ||
|
||
function intFromBytesWithCheckPanic(byte[] bytes) returns int { | ||
string str = checkpanic string:fromBytes(bytes); | ||
return checkpanic int:fromString(str); | ||
} | ||
|
||
// same as intFromBytesWithCheckPanic but with explicit panic statements | ||
function intFromByteWithExplictPanic(byte[] bytes) returns int { | ||
string|error res = string:fromBytes(bytes); | ||
if res is error { | ||
panic res; | ||
} | ||
int|error val = int:fromString(res); | ||
if val is error { | ||
panic val; | ||
} | ||
return val; | ||
} | ||
|
||
public function main() { | ||
int|error res = intFromBytes([104, 101, 108, 108, 111]); | ||
io:println(res); | ||
int|error res1 = intFromBytesWithCheck([104, 101, 108, 108, 111]); | ||
io:println(res1); | ||
int|error res2 = intFromBytesExplicit([104, 101, 108, 108, 111]); | ||
io:println(res2); | ||
int|error res3 = trap intFromBytesWithCheckPanic([104, 101, 108, 108, 111]); | ||
io:println(res3); | ||
int|error res4 = trap intFromByteWithExplictPanic([104, 101, 108, 108, 111]); | ||
io:println(res4); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
# Check expression | ||
|
||
`check E` is used with an expression `E` that might result in an `error` value. If `E` results in an `error` value , then, `check` makes the function return that `error` value immediately. | ||
|
||
The type of `check E` does not include `error`. The control flow remains explicit. | ||
In ballerina it is common to write expression that may result in an error, such as calling a function that could return an error value, check if the result belong to `error` type and immediately return that value. You can use `check` expression to simplify this pattern. | ||
|
||
Similarly by the context of your code you may know that a certain expression should not result in an error, in such cases you can use `checkpanic` expression to immediately panic if the result is an error. | ||
::: code check_expression.bal ::: | ||
|
||
::: out check_expression.out ::: | ||
::: out check_expression.out ::: | ||
|
||
## Related links | ||
- [Panics](https://ballerina.io/learn/by-example/panics/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
$ bal run check_expression.bal | ||
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'") | ||
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'") | ||
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'") | ||
error("{ballerina/lang.int}NumberParsingError",message="'string' value 'hello' cannot be converted to 'int'") |