-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2031 from sanjiva/master
Clean up iterators and moved exceptions to its own section
- Loading branch information
Showing
5 changed files
with
97 additions
and
61 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,17 @@ | ||
# Documentation and Comments | ||
|
||
Many programming languages treat documentation as an after thought with some languages using the approach of introducing structure to comments to insert structured documentation into the language. | ||
|
||
Ballerina has a structured approach for both documentation and random comments which are not related to documenting per se but simply commenting about the logic of the program. | ||
|
||
## Documenting with annotations | ||
|
||
> NOTE: To be written to explain docerina architecture. | ||
## Commenting Code | ||
|
||
Any statement that starts with the characters `//` is a comment. | ||
|
||
|
||
Comments are quite different in Ballerina in comparison to other languages. Comments are only allowed as a statement - i.e., only inside a resource, action, or function. | ||
Ballerina has designed structured mechanisms via annotations to document all Ballerina outer level constructs (services, resources, etc.), and comments only play the role of providing a comment about the logic of a resource, action, or function. |
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,46 @@ | ||
# Exception Handling | ||
|
||
Ballerina supports throwing and catching exceptions using a simple exception handling model. This is supported by the `exception` type and the constructs `try-catch` and `throw`. | ||
|
||
## Behavior | ||
|
||
An exception may be thrown by a native Ballerina function or any Ballerina construct using the `throw` statement. When thrown, the runtime searches for the nearest enclosing block containing a `try-catch` statement. If none is found in the current stack frame then execution of the function (or resource or action or type mapper) stops and the frame is popped and the search continues until a `try-catch` statement is found. If none is found at the outermost level of the worker, then that worker thread dies in an abnormal state. | ||
|
||
If the exception goes through the default worker of a `main` function without being caught then the entire program will stop executing. If the exception goes through a `resource` without being caught then that particular invocation of the service & resource will fail and the server connector will choose the appropriate behavior in that situation. | ||
|
||
# The `exception` Type | ||
|
||
Exceptions are instances of a built-in, opaque reference type named `exception`. | ||
|
||
A collection of library functions can be used to set and get properties of exceptions, including stack traces. Note that unlike other languages, Ballerina does not allow developers to define subtypes of the exception type and custom exceptions must be thrown by using custom category strings. As such exception category strings starting with "Ballerina:" are reserved for system use only. | ||
|
||
Variables of type `exception` are defined and initialized to a new exception as follows: | ||
``` | ||
exception VariableName = {}; | ||
``` | ||
Library functions for accessing information from this type are in the package `ballerina.lang.exceptions`. These functions can be used to set the category of the exception, the descriptive messsage and any additional properties. | ||
|
||
# The `try-catch` Statement | ||
|
||
The syntax of a `try-catch` is as follows: | ||
``` | ||
try { | ||
Statement;+ | ||
} catch (exception e) { | ||
Statement;+ | ||
} | ||
``` | ||
If any exception occurs while execution the first block of statements then the exception will be handled by the block after the `catch`. | ||
|
||
> NOTE: Ballerina currently does not have a `finally` concept but we will likely add it. | ||
# The `throw` Statement | ||
|
||
The syntax of a `throw` statement is as follows: | ||
``` | ||
throw ExceptionVariableName; | ||
``` | ||
|
||
The `throw` statement is used to throw an exception from the current location. An execution stack trace pointing to the current location will be automatically inserted into the exception before the runtime starts the exception handling process. | ||
|
||
> NOTE: There will be more capabilities brought to exceptions in future. |
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