-
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
8b9148c
commit 370e806
Showing
5 changed files
with
46 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import ballerina/io; | ||
|
||
function hereBeDragons() returns int { | ||
// Trigger a panic deep within the call stack. | ||
alwaysPanic(); | ||
} | ||
|
||
// Return type `never` indicate that this function will never return normally. That is it will always panic | ||
function alwaysPanic() returns never { | ||
panic error("deep down in the code"); | ||
} | ||
|
||
public function main() { | ||
// Division by 0 trigger a panic | ||
int|error result = trap 1 / 0; | ||
if result is error { | ||
io:println("Error: ", result); | ||
} | ||
// This will tigger a panic deep within the call stack and we'll trap it here | ||
int|error result2 = trap hereBeDragons(); | ||
if result2 is error { | ||
io:println("Error: ", result2); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Trap expression | ||
|
||
If you have an expression such as a function call that can potentially trigger a `panic` you can use a `trap` expression to prevent further unwinding of the stack. Then if the evaluation of the expression trigger a panic you will get the `error` associated with the panic. Otherwise you will get the result of the expression. | ||
|
||
::: code trap_expression.bal ::: | ||
|
||
::: out trap_expression.out ::: | ||
|
||
+ [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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
description: This BBE demonstrates how the trap expression is used in Ballerina to handle panics | ||
keywords: ballerina, ballerina by example, bbe, error, panic, trap |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$ bal run trap_expression.bal | ||
Error: error("{ballerina}DivisionByZero",message=" / by zero") | ||
Error: error("deep down in the code") | ||
|